Home >Backend Development >PHP Problem >How to delete characters at the end of string in php
We know that there are several ways to delete characters from a string. Today we will introduce three PHP functions to delete the last character of a string. Friends in need can refer to them.
Method 1: substr() function
substr()
The function returns a part of the string.
The syntax is as follows:
substr(string string, int start, int [length]);
Parameter explanation:
<?php $str = "1,2,3,4,5,6"; $newstr = substr($str,0,strlen($str)-1); echo $newstr; ?>Result:
1,2,3,4,5,You can see that the output result
$strThe last character is gone.
Method 2: substr_replace() function
substr_replace()The function replaces part of a string with another string.
substr_replace()(string string, int start, int [length]);Parameter explanation:
$email='peter@qq.com'; echo substr_replace($email,'',11,1);The output result is:
peter@qq.coThe last character m is deleted.
Method 3: rtrim() function
If the last character of the string is a blank character or a special character, you can usertrim( ) function, and we only need one parameter, but if the last character is not a blank character or a special character, we need to specify the deleted character.
string rtrim ( string $str [, string $character_mask ] )Parameter explanation:
<?php $email='peter@qq.com'; echo rtrim($email,"m"); ?>The output result is:
peter@qq.coRecommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to delete characters at the end of string in php. For more information, please follow other related articles on the PHP Chinese website!