In this article, you will learn how to use PHP to remove the last character from a string. Also, you will be able to replace it with another char using some of the PHP predefined functions such as substr_replace and substr.
Table Of Contents
The PHP string is a list of characters quotes with single or double quotations the maximum of characters should not be over 256 characters.
however, PHP does not support the native Unicode. The string can be set inside the double-quotes or single-quotes. The question is: How to remove the last character from a string using PHP?

In the next sections, we are going to explain more than a way to do that.
Remove the Last Char from a String using substr_replace with PHP
Before starting on how to remove the last char from the string you have to understand the substr_replace function first.
The substr_replace is a PHP function that already replaces a specific part of the string with another string, and that can work according to the index of the selected part.
Anyway, the substr_replace takes four parameters, the first three parameters are required but the last one is optional. In the following list, we are going to explain each parameter in a few words.
<?php
substr_replace( $string, $new_value, $started_index, $char_length );
?>
- The $string is the main whole string variable.
- The $new_value is the new part that replaces its values instead of the old one.
- The $started_index is the starting position for the new part.
- The $char_length is optional and it is specifying the number of characters that have to change their values. If it doesn’t exist so the number of the new characters will be added instead.
Let’s see how to change the last char in the string using the substr_replace function.
Before starting I have to explain another thing regarding the $started_index. If the value is negative so that means it will start from the end.
In the following code, I need to remove the last character “/” from the URL.
<?php
$url = "https://codedtag.com/tutorials/php//";
echo substr_replace( $url, "", -1 );
?>
The output would be like the following
https://codedtag.com/tutorials/php/
But the question is: how to replace the string with another part? The answer is so simple you have to put the new value in the empty double quotations which is called the $new_value parameter.
Remove the Last Char from a String using substr with PHP
Before explaining how to remove a part from the string using the substr function you have to understand what does it do?
the substr is a predefined PHP function that returns a specific part from the main string and takes three parameters.
<?php
substr( $string, $started_index, $char_length );
?>
- The $string is the main whole string variable.
- The $started_index is the target index that needed to be a starting position for the new part.
- The $char_length is specifying the number of characters that have to be declined. it cab be a negative value.
So the difference between the substr_replace and substr PHP functions. the first one is replacing another string instead of the old part but the substr is returning only the target part from the string.
Let’s see how to return the needed URL value from the previous PHP code so we will decline the last char in this function.
<?php
$url = "https://codedtag.com/tutorials/php//";
echo substr( $url, 0, -1 );
?>
Remove the Last Char from a String using mb_substr with PHP
The mb_substr is returning a part from the string and it takes four parameters such as $string, $start, $length, and $encoding.
Let’s see how to remove the last char from the string using mb_substr.
<?php
$url = "https://codedtag.com/tutorials/php//";
echo mb_substr( $url, 0, -1 );
?>
Wrapping Up
You learned how to remove the last character from the PHP string and learned how to apply that in the two functions substr_replace, substr and mb_substr.
Also, you learned how to replace a specific value in the string according to the index of position using the two previous functions.
To learn more about string type follow the PHP manual.