Home  >  Article  >  Backend Development  >  Use substr() to remove the last character of a string

Use substr() to remove the last character of a string

WBOY
WBOYOriginal
2016-07-25 09:05:432354browse
When making statistical charts, you need to use "," to separate the data when fetching it. However, when I use a loop to accumulate strings, it is inevitable that there will be an extra "," in the end. For example, I took out 1,2,3,4,5, but I need to delete the comma at the end of the string to get 1,2,3,4,5.

When making statistical charts, you need to separate the data with "," when extracting it. However, when I use a loop to accumulate strings, it is inevitable that there will be an extra "," in the end. For example, I took out 1,2,3,4,5, but I need to delete the comma at the end of the string to get 1,2,3,4,5. Solution: Using php’s substr method, Syntax: string substr(string string, int start, int [length]); Parameter 1: original string; Parameter 2: starting position of cutting; Parameter 3: intercepted length; Use it like this: $newstr = substr($str,0,strlen($str)-1); Cut from the beginning to the penultimate digit, thus removing the last ",".



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