How to remove the last character from a PHP string?

You can utilize the substr() function to remove the last character.

The sample code is as follows:

$str = "Hello World!";
$newStr = substr($str, 0, -1);
echo $newStr;

The output is:

Hello World

The third parameter in the substr() function, if negative, counts from the end of the string, so setting it to -1 can remove the last character.

bannerAds