Home  >  Article  >  Backend Development  >  How to use substr() to remove the last character of a string in php_PHP tutorial

How to use substr() to remove the last character of a string in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:15843browse

Used in today’s project to remove the last character in the string
Original string 1,2,3,4,5,6,
Remove the last character "," and the final result is 1,2,3,4,5,6
The code is as follows:

The code is as follows:


$str = "1,2,3,4,5,6,";
$newstr = substr($str,0,strlen($str)-1);
echo $newstr;


The system’s own functions can also achieve this effect, two methods:
1) substr($str, 0, -1)
2)rtrim($str, ",")

substr
Get part of the string.
Syntax: string substr(string string, int start, int [length]);
Return value: string
Function type: Data processing
Content Description
This function extracts length characters from the start position of the string string. If start is a negative number, it starts from the end of the string. If the omitted parameter length exists but is a negative number, it means that the length character from the bottom is obtained.
Usage examples

The code is as follows:


echo substr("abcdef", 1, 3); // Return "bcd"
echo substr("abcdef", -2); // return "ef"
echo substr("abcdef", -3, 1); // return "d"
echo substr("abcdef", 1, -1); // return "bcde"
?>


PHP rtrim() function
Definition and Usage
The rtrim() function removes whitespace characters or other predefined characters starting from the end of a string. Same as chop() function.
Grammar
rtrim(string,charlist) parameter description
string required. Specifies the string to be converted.
charlist Optional. Specifies which characters are removed from the string.
If this parameter is not set, the following characters are all removed:
" "t" - ASCII 9, tab
"n" - ASCII 10, new line
"x0B" - ASCII 11, vertical tab
"r" - ASCII 13, carriage return
" " - ASCII 32, space

Usage examples

php去掉字符串的最后一个字符substr()的用法 帮客之家

http://www.bkjia.com/PHPjc/756103.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/756103.htmlTechArticle Used in today’s project, remove the last character in the string and the original string 1,2,3,4 ,5,6, remove the last character, and the final result is 1,2,3,4,5,6. The code is as follows: $str =...
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