Used in today's project, remove the last character in the string
Original string 1,2,3,4,5,6,
Remove the last character ",", the final result is 1,2,3,4, 5,6
The code is as follows:
Copy the code The code is 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: Original string;
Parameter 2: starting position of cutting;
Parameter 3: intercepted length;
Use as follows:
$newstr = substr($str,0,strlen($str)-1);
From Intercept from the beginning and continue to the penultimate digit, thus removing the last ",".
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 takes out the length of the string starting from the start position of the string string characters. 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 example
Copy code 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
rtrim() function removes whitespace characters or other predefined characters from the end of a string. Same as chop() function.
Syntax
Parameters |
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, all the following characters will be deleted:
-
"
The above introduces the use of substr php to remove the last character of a string and append substr, including the content of substr. I hope it will be helpful to friends who are interested in PHP tutorials.
-
|