This tutorial is all about how to remove last character from the string in PHP. We will understand it with the help of 4 inbuilt PHP functions.
Sometimes we need to eliminate or remove the some part of the string like special characters or something in the beginning and ending of the string. For example we need to perform some loop or activity in our web application in a condition when 5 character of string meet or equal to some particular criteria. In that condition we need to extract the initial 5 characters from the string map that to with our criteria , if condition matched, we can perform some activity. and the same thing we can do with by remove last two or three characters from the string. Kind of situation arise in an application when we need to remove last two characters, last three characters or last four characters and so on from the string. So let’s discuss them all in the below and understand by using 4 different functions.
Checkout more articles on PHP
How to Partially Hide Phone in PHP?
How to check an array is multidimensional or not in PHP?
PHP Multidimensional Array Search by Key and Value with Examples
How to combine two strings in PHP
Using substr_replace function
Use the substr_replace function to remove the last character from a string in PHP. Its a inbuilt PHP function. It takes three arguments substr_replace(string, replace value, length). In the last parameter if we keep it positive it will count the value from the left hand side of the string and in negative it counts from the right of the string and replace it with the second parameters given in the function.
Syntax:
substr_replace($string ,"", -1);
Example:
<?php $string = "Hello Atcodex!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . substr_replace($string ,"",-1) . "\n"; ?>
Output:
Original string: Hello Atcodex!
Updated string: Hello Atcodex
Using substr function
With the help of substr function we can remove the last character from any string in PHP. It accepts three parameters substr(string, initial position of the string, final position of the string). You can remove the last two character from the string by just changing the -1 to -2 in the function. Difference between substr_replace() and substr() is substr_replace replace the value with its second parameters and substr extract the value from the string.
Syntax:
substr($string, 0, -1);
Example:
<?php $string = "Hello Atcodex!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . substr($string, 0, -1) . "\n"; ?>
Output:
Original string: Hello Atcodex!
Updated string: Hello Atcodex
Using mb_substr function
Use the mb_substr function to remove characters from the end of the string.
Syntax:
mb_substr($string, 0, -1);
Example:
<?php $string = "Hello Atcodex!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . mb_substr($string, 0, -1) . "\n"; ?>
Output:
Original string: Hello Atcodex!
Updated string: Hello Atcodex
Using rtrim function
The rtrim function to used to remove specific characters from the end of the string. rtrim() function remove the character from the right end of the string. in takes two arguments rtrim(string, trim value)
Syntax:
rtrim($string,'x');
Here “x” is the character to remove.
Example:
<?php $string = "Hello Atcodex!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . rtrim($string, "!") . "\n"; ?>
Output:
Original string: Hello Atcodex!
Updated string: Hello Atcodex
Conclusion:
We have tried to explain how you can extract some part of string using PHP with example. You can use any of the above function to solve the problem. Hope you liked the article.