How to remove characters at the end of string in php

王林
Release: 2023-03-05 17:50:02
Original
3816 people have browsed it

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)].

How to remove characters at the end of string in php

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)
Copy after login

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,&#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

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!

Related labels:
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