string trim ( string $str [, string $charlist ] ) - Remove whitespace characters at the beginning and end of a string (or other characters)
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
Copy code The code is as follows:
1) trim(' "string"', '"sg'); // Final output: "strin
2) trim(' "string" ', '"sg'); // Final output: "string"
2)trim('"string"', '"sg'); // Final output: trin
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.
Example 1
Output:
<?php $str = " 使用函数trim去掉字符串两端空白字符 "; $str1 = trim($str); echo "处理前有".strlen($str)."个字符"; echo "<br/>"; //www.phpjc.cn echo "<br/>"; echo "使用trim函数处理后有".strlen($str1)."个字符"; ?>
There are 39 characters before processing and there are 34 characters after processing using PHP function trim()
Example 2
Output:
<?php $str = "##使用函数trim去掉字符串两端特定字符####"; $str1 = trim($str,"#"); //为函数trim传入第二个参数, trim将删除字符串$str两端的#字符 echo $str."<br>"; echo $str1; ?>
##Use the PHP function trim() to remove specific characters at both ends of the string#### Use the function trim() to remove specific characters at both ends of the string
The above is the entire content of this article, I hope you all like it.
http://www.bkjia.com/PHPjc/985279.html