Php remove last character from string

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

Contrary to the question asked, rtrim[] will remove any number of characters, listed in the second argument, from the end of the string. In case you expect just a single comma, the following code would do:

$newarraynama = rtrim[$arraynama, ","];

But in my case I had 2 characters, a comma and a space, so I had to change to

$newarraynama = rtrim[$arraynama, " ,"];

and now it would remove all commas and spaces from the end of the string, returning a, b, c, d, e either from a, b, c, d, e,, a, b, c, d, e,,,, a, b, c, d, e, or a, b, c, d, e , ,, , ,

But in case there could be multiple commas but you need to remove only the last one, then rtrim[] shouldn't be used at all - see other answers for the solution that directly answers the question.

However, rtrim[] could be a good choice if you don't know whether the extra character could be present or not. Unlike substr-based solutions it will return a, b, c, d, e from a, b, c, d, e

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:

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:

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:

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:

Output:

Original string: Hello TecAdmin!
Updated string: Hello TecAdmin

How can I remove last character 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 5 characters from a string in PHP?

How to Remove the Last Character from a String in PHP.
The First Option is Substr Function..
The Second Option is Substr_replace Function..
The Third Option is Rtrim[] Function..

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 do I remove the last text from a string?

Using String. The easiest way is to use the built-in substring[] method of the String class. 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.

Chủ Đề