How to remove the end of string in php

藏色散人
Release: 2023-03-05 19:08:01
Original
2443 people have browsed it

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.

How to remove the end of string in php

Recommendation: "PHP Video Tutorial"

php Remove the characters at the end of the string

$str = '123,234,345,';
echo $str,&#39;<br/>&#39;;
//1、使用substr()函数实现去除字符串末尾字符
echo substr($str,0,-1),&#39;<br/>&#39;;//123,234,345
 
 
//2、使用rtrim()函数实现去除字符串末尾字符
//2.1、已知末尾字符
echo rtrim($str,&#39;,&#39;),&#39;<br/>&#39;;//123,234,345
 
//2.2、不知道末尾字符,需先获取末字符
$last_char=$str{strlen($str)-1}; // &#39;,&#39;
//或者: $last_char=substr($str,-1); // &#39;,&#39;
echo rtrim($str,$last_char);//123,234,345
Copy after login

Supplement:

Several uses of curly braces in php:

//1、 {} 表示程序块的开始和结束:if{} while{} switch(){}等
 
//2、 {}用来表示字符串下标
$str = &#39;hello&#39;;
echo $str[0],&#39;<br>&#39;;// h
echo $str{0},&#39;<br>&#39;;// h
// 在字符串的变量的后面跟上{}花括号和方括号一样都是把某个字符串变量当成数组处理
 
//3、分离变量
$s=&#39;aaa&#39;;
echo "${s}bbb<br/>";//等同于"$s.bbb" => "aaa.bbb" 结果输出:aaabbb
// 防止变量名和后面的字符串合并成一个变量名,变成$aaabbb
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!