In PHP, you can use the PHP built-in function "rtrim()" to remove the characters at the end of the string. The rtrim function can directly remove the blank characters or other predefined characters on the right side of the string and return the modified String, its syntax is [rtrim($str,$last_char)].
rtrim() function removes whitespace characters or other predefined characters on the right side of a string and returns the modified string.
(Recommended tutorial: php video tutorial)
Syntax:
rtrim(string,charlist)
Parameter introduction:
string Required. Specifies the string to check.
#charlist Optional. Specifies which characters are removed from a string.
(Related recommendations: php training)
Code implementation:
$str = '123,234,345,'; echo $str,'<br/>'; //1、使用substr()函数实现去除字符串末尾字符 echo substr($str,0,-1),'<br/>';//123,234,345 //2、使用rtrim()函数实现去除字符串末尾字符 //2.1、已知末尾字符 echo rtrim($str,','),'<br/>';//123,234,345 //2.2、不知道末尾字符,需先获取末字符 $last_char=$str{strlen($str)-1}; // ',' //或者: $last_char=substr($str,-1); // ',' echo rtrim($str,$last_char);//123,234,345
The above is the detailed content of How to remove characters at the end of string in php. For more information, please follow other related articles on the PHP Chinese website!