Home >Backend Development >PHP Problem >How to remove the end of string in php
php method to remove the last character of the string: 1. Use the substr function to remove the last character of the string; 2. Use the rtrim function to remove the last character of the string; 3. Use "rtrim($str,$last_char) ;" method removes characters at the end of a string.
Recommendation: "PHP Video Tutorial"
php Remove the characters at the end of the string
$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
Supplement:
Several uses of curly braces in php:
//1、 {} 表示程序块的开始和结束:if{} while{} switch(){}等 //2、 {}用来表示字符串下标 $str = 'hello'; echo $str[0],'<br>';// h echo $str{0},'<br>';// h // 在字符串的变量的后面跟上{}花括号和方括号一样都是把某个字符串变量当成数组处理 //3、分离变量 $s='aaa'; echo "${s}bbb<br/>";//等同于"$s.bbb" => "aaa.bbb" 结果输出:aaabbb // 防止变量名和后面的字符串合并成一个变量名,变成$aaabbb
The above is the detailed content of How to remove the end of string in php. For more information, please follow other related articles on the PHP Chinese website!