Php removes the usage of the last character substr () of the string. In today's project, remove the last character in the string from the original string 1, 2, 3, 4, 5, and 6. The final result is 1, 2, 3, 4, 5, and 6. the code is as follows: $ str is 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 built-in functions of the system can also achieve the following two methods:
1) substr ($ str, 0,-1)
2) rtrim ($ str ,",")
Substr
Take some strings.
Syntax: string substr (string, int start, int [length]);
Return value: string
Function type: data processing
Description
This function extracts the start character of the string from the start character. If start is a negative number, it is counted from the end of the string. If the parameter length can be omitted, but it is a negative number, it indicates that the maximum length is obtained.
Example
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 white spaces or other predefined characters from the end of a string. The same as the chop () function.
Syntax
Rtrim (string, charlist) parameter description
String is required. Specifies the string to be converted.
Charlist is optional. Specifies which characters are deleted from the string.
If this parameter is not set, all of 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
Example
Remove the last character from the original string 1, 2, 3, 4, 5, 6. The final result is 1, 2, 3, 4, 5, and 6. the code is as follows: $ str =...