Home > Article > Backend Development > A simple guide to using the trim() function in PHP_PHP Tutorial
string trim ( string $str [, string $charlist ] ) - Remove blank characters (or other characters) at the beginning and end of a string )
When the second parameter is empty, the trim() function will remove spaces, tabs, line feeds, carriage returns, vertical tabs, etc. by default. When the second parameter is added
The code is as follows:
Therefore, the trim() function first removes the blank characters at the beginning and end of the characters, and then filters out the given characters (list) to be removed. It is also applicable to the ltrim() and rtrim() functions
Definition and usage
The PHP function trim() removes whitespace characters and other predefined characters from both ends of a string.
Grammar
trim(str,charlist) Parameter 1 str is the string to be operated, parameter 2 charlist is optional and specifies the special symbols to be removed.
If the second parameter is not given a value, the following characters will be removed by default:
" " (ASCII 32 (0x20)), an ordinary space.
"t" (ASCII 9 (0x09)), a tab.
"n" (ASCII 10 (0x0A)), a new line (line feed).
"r" (ASCII 13 (0x0D)), a carriage return.
"
"x0B" (ASCII 11 (0x0B)), a vertical tab.
If you want to remove other characters, you can set it in the second parameter.
1
4 5 6
7 |
echo "There are ".strlen($str)." characters before processing"; echo " "; //www.phpjc.cn echo " ";
|
1 2 3 4 5 6 7 |
<🎜>$str = "##Use function trim to remove specific characters at both ends of the string####";<🎜>
<🎜>$str1 = trim($str,"#");<🎜>
<🎜>//Pass in the second parameter to the function trim, <🎜>
<🎜>trim will delete the # characters at both ends of the string $str echo $str." "; echo $str1; ?> |