PHP removes the use of substr of from the last character of the string

  • 2020-03-31 21:34:12
  • OfStack

For today's project, 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:
 
$str = "1,2,3,4,5,6,"; 
$newstr = substr($str,0,strlen($str)-1); 
echo $newstr; 


Reading:
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: length of interception;

Use it like this:
$newstr = substr ($STR 0, strlen ($STR) - 1);
I'm going to cut it from the beginning, all the way down to the bottom, and I'm going to get rid of the last ",".


The functions of the system can also achieve this effect, two methods:
1) substr($STR, 0, -1)
2) the rtrim ($STR. ", ")

substr
Take a partial string.
Syntax: string substr(string string, int start, int [length]);
Return value: string
Function type: data processing
Content description
This function fetches the length character from the start bit of a string. If start is negative, start at the end of the string. If the omitted parameter length exists, but is negative, it means that the penultimate length character is taken.
Use examples
 
<? 
echo substr("abcdef", 1, 3); //Return "BCD"
echo substr("abcdef", -2); //Return "ef"
echo substr("abcdef", -3, 1); //Return to the "d"
echo substr("abcdef", 1, -1); //Return "bcde"
?> 


PHP rtrim () function

Definition and usage
The rtrim() function deletes white space or other predefined characters from the end of the string. Chop ().

grammar
parameter describe The string A necessity. Specifies the string to be converted. charlist

Optional. Specifies which characters are removed from the string.

If this parameter is not set, all the following characters are deleted:

"\0" - ASCII 0, NULL "\t" - ASCII 9, TAB "\n" - ASCII 10, new line "\x0B" - ASCII 11, vertical TAB "\r" - ASCII 13, press enter "" - ASCII 32, space
Use examples
< img border = 0 SRC = "http://files.jb51.net/upload/201103/20110323213415222.png" border = 0 >

Related articles: