PHP Remove last character from string

Want to remove the last character from your text. Just use Substr_Replace. This is how.

PHP’s substr_replace replaces text within a portion of a string. In this case we only want to replace the last character with nothing. Here is the code sample:

$old_string = "My-Category-";
// remove the last hyphen from the string
$new_string = substr_replace($old_string,"",-1);
echo $new_string;
// should now read "My-Category"

This should replace the last character of a string with nothing.

3 Responses

  1. Pingback: Remove a character from the end of a string « Copy // Paste // Modify

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Next Post » »