-11 Vote

PHP: Remove last Character from String

Question by Guest | 2015-06-26 at 13:33

Currently, I am searching for a possibility to cut off the last character from a string using PHP.

This action should be carried out independent from the character that stands at the end of the string. In other words, any arbitrary character should be removed from the end.

Is PHP providing any function for this? Thank you very much.

ReplyPositiveNegative
0Best Answer0 Votes

You can use the PHP function substr() for this purpose.

In order to remove the last character from the string, you have to pass 0 as first parameter and -1 as second parameter.

$s = "abcde";

echo substr($s, 0, -1);  // abcd

The second parameter is specifying from which character position the string should be copied. The first character is the character 0 and because we do not want to remove anything from the front, we are accordingly using 0 as the second parameter.

The third parameter is specifying the length of the substring. A negative value means that it is counted from behind. That is why we are passing -1 to get the last character.
2015-06-27 at 10:31

ReplyPositive Negative
Reply

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.