How can i remove last character from a string in php?

How can i remove last character from a string in php?
This is a very common PHP question, how to remove last character from string in PHP?

So here are three ways how to delete last character from string in PHP.

Method 1 – PHP: Remove Last Character from String using substr and mb_substr

substr and mb_substr commands usage


substr($string, 0, -1);
mb_substr($string, 0, -1);

substr and mb_substr example:


$string = "This is test string..";
echo $string . "\n";

// substr function
echo "substr: " . substr($string, 0, -1);

echo "\n";

// mb_substr multibyte version
echo "mb_substr: " . mb_substr($string, 0, -1);

echo "\n";

Example output:

This is test string..
This is test string.
This is test string.

substr_replace command usage


substr_replace($string ,"",-1);

substr_replace example:


$string = "This is test string..";
echo $string . "\n";

// substr_replace function
echo "substr_replace: " . substr_replace($string ,"",-1);

echo "\n";

Example output:

This is test string..
This is test string.

Method 3 – PHP: Remove Last Character from String using rtrim

Note: rtrim function is not working exactly same way as substr and substr_replace, but it is of course useful some cases. Rtrim function trims all specified characters from end of the string.

rtrim command usage


rtrim($string,'x');

rtrim example:


$string = "This is test string..";
echo $string . "\n";

// rtrim function
echo "rtrim: " . rtrim($string, ".");

echo "\n";

Example output:

This is test string..
This is test string

Question – How do I remove last character from a string in PHP script? In this tutorial you will learned multiple methods to remove last character from a string using php.

This tutorial describes 4 methods to remove last character from a string in PHP programming language. You can use any one of the following methods as per the requirements.

  • Don’t Miss – Check If String Contains a Sub String in PHP

Method 1 – Using substr_replace function

You can use the PHP substr_replace() function to remove the last character from a string in PHP.

Syntax:

substr_replace($string,"",-1);

Example:

$string="Hello TecAdmin!";

echo"Original string: ".$string."\n";

echo"Updated string: ". substr_replace($string,"",-1)."\n";

?>

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Method 2 – substr function

Use the substr function to remove the last character from any string in PHP string

Syntax:

Example:

$string="Hello TecAdmin!";

echo"Original string: ".$string."\n";

echo"Updated string: ". substr($string,0,-1)."\n";

?>

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Method 3 – mb_substr function

Use the mb_substr function to remove characters from the end of the string.

Syntax:

mb_substr($string,0,-1);

Example:

$string="Hello TecAdmin!";

echo"Original string: ".$string."\n";

echo"Updated string: ". mb_substr($string,0,-1)."\n";

?>

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

Method 4 – rtrim function

The rtrim function to used to remove specific characters from the end of the string.

Syntax:

Here “x” is the character to remove.

Example:

$string="Hello TecAdmin!";

echo"Original string: ".$string."\n";

echo"Updated string: ". rtrim($string,"!")."\n";

?>

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

How do I remove the last character of a string?

In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character. We can achieve that by calling String's length() method, and subtracting 1 from the result.

How can I remove last 5 characters from a string in PHP?

You can use the PHP substr_replace() function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .

How can I remove last three characters from a string in PHP?

To remove the last three characters from a string, we can use the substr() function by passing the start and length as arguments.

How can I remove one character from a string in PHP?

Explanation: In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known. $str = "geeks" ; // Or we can write ltrim($str, $str[0]);