Home  >  Article  >  Backend Development  >  How to remove characters at the end of string in php

How to remove characters at the end of string in php

王林
王林Original
2020-09-03 14:46:533710browse

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)

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

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!

Statement:
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