PHP - How to replace the last character of a string?

Replacing the last character of a String in PHP can be done using the substr_replace() function.

For example, if the String is "uhded-" and you want to get rid of the last character. You can do substr_replace("uhded-","",-1);


$str = "uhded-";

echo substr_replace($str,"",-1);

//returns "uhded"

The way it works is that when you specify the third parameter with a -1, it starts replacing from the back of the String.

I hope this helps.

Want to learn how to code and make money online? Check out CodingPhase (referral link)