Home >Backend Development >PHP Tutorial >How to remove last character of string using php
There are many ways to delete the last character of a string in How to remove last character of string using php. This article will introduce to you four methods of deleting the last character of a string in How to remove last character of string using php. Friends in need can refer to it.
Method 1: substr_replace function
Use the substr_replace function to remove the last character from a string in PHP.
The basic syntax is as follows:
substr_replace($string ,"", -1);
The example is as follows
$string = "Hello admin!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . substr_replace($string ,"",-1) . "\n";
The running effect is as follows:
Original string: Hello admin! Updated string: Hello admin
Method 2: substr function
Use the substr function to remove the last character from any string in PHP string
The basic syntax is as follows
substr($string, 0, -1);
The example is as follows
$string = "Hello Admin!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . substr($string, 0, -1) . "\n";
Run The effect is as follows:
Original string: Hello admin! Updated string: Hello admin
Method 3: mb_substr function
Use the mb_substr function to delete characters from the end of the string.
The basic syntax is as follows
mb_substr($string, 0, -1);
The example is as follows
$string = "Hello Admin!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . mb_substr($string, 0, -1) . "\n";
The running effect is as follows
Original string: Hello Admin! Updated string: Hello Admin
Method 4: rtrim function
rtrim function is used to delete specific characters from the end of a string
The basic syntax is as follows
rtrim($string,'x');
Here "x" is the character to be deleted.
The example is as follows
$string = "Hello TecAdmin!"; echo "Original string: " . $string . "\n"; echo "Updated string: " . rtrim($string, "!") . "\n";
The running result is as follows
Original string: Hello Admin! Updated string: Hello Admin
This article ends here. For more exciting content, you can pay attention to How to remove last character of string using php Chinese Other related column tutorials on the Internet! ! !
The above is the detailed content of How to remove last character of string using php. For more information, please follow other related articles on the PHP Chinese website!