Used today in the project to remove the last character in the string
The original string 1,2,3,4,5,6,
Remove the last character "," and the end result is 1,2,3,4,5,6
The code is as follows:
Copy Code code as follows:
$str = "1,2,3,4,5,6,";
$newstr = substr ($str, 0,strlen ($STR)-1);
Echo $newstr;
Interpretation:
Using PHP's Substr () method,
Syntax: String substr (string string, int start, int [length]);
Parameter 1: the original string;
Parameter 2: The starting position of the cutting;
Parameter 3: Length of interception;
Use this to:
$newstr = substr ($str, 0,strlen ($STR)-1);
Intercept from the beginning, always intercept to the penultimate, so that the final "," removed.
The system's own functions can also achieve this effect, two ways:
1) substr ($str, 0,-1)
2) RTrim ($str, ",")
substr
Takes a partial string.
Syntax: String substr (string string, int start, int [length]);
return value: String
Function type: Data processing
Content Description
This function extracts the length character of the string from the start of string strings. If start is a negative number, it is counted from the end of the string. If the parameter length that can be omitted exists but is negative, it is taken to the penultimate length character.
Usage examples
Copy Code code as follows:
?
echo substr ("abcdef", 1, 3); Return "BCD"
echo substr ("ABCdef",-2); Return to "EF"
echo substr ("ABCdef",-3, 1); Return "D"
echo substr ("abcdef", 1,-1); Return to "BCDE"
?>
PHP RTrim () function
Definitions and usage
The RTrim () function deletes white space characters or other predefined characters from the end of the string. Same chop () function.
Grammar
Parameters |
Description |
String |
Necessary. Specify the string to convert. |
Charlist |
Optional. Specify which characters to remove from the string. If this argument is not set, all the following characters are deleted:
- "I"-ASCII 0, NULL
- "\ T"-ASCII 9, Tab
- "\ n"-ASCII 10, New line
- "\x0b"-ASCII 11, Vertical tab
- "\ r"-ASCII 13, carriage return
- ""-ASCII 32, space
|
Usage examples