In PHP, you can use the rtrim function to remove the last space. The syntax is "rtrim(string,charlist)", and the parameter string specifies the string to be checked.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to remove the last space in php?
You can use the rtrim function to remove the last space. rtrim is to remove the right space of a string.
rtrim() function removes whitespace characters or other predefined characters on the right side of a string.
Related functions:
ltrim() - Removes whitespace characters or other predefined characters on the left side of the string
trim() - Removes both sides of the string White space characters or other predefined characters
Syntax
rtrim(string,charlist)
Parameters
string Required. Specifies the string to check.
charlist
Optional. Specifies which characters are removed from the string. If omitted, all of the following characters are removed:
"\0" - NULL
"\t" - Tab
"\n" - Newline
"\x0B" - vertical tab
"\r" - carriage return
" " - space
Return value: Return the modified character string.
Remove spaces on the right side of the string:
<?php $str = "Hello World! "; echo "不使用 rtrim:" . $str; echo "<br>"; echo "使用 rtrim:" . rtrim($str); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the last space in php. For more information, please follow other related articles on the PHP Chinese website!