String Trim (String $str [, String $charlist])-To remove white space characters (or other characters) at the end of a string
The trim () function removes spaces, tabs, line breaks, carriage returns, vertical tabs, and so on, by default when the second argument is empty, when the second argument is added
Copy Code code as follows:
1 Trim (' "string\" ', ' "SG '); Final output: \ "Strin
2 Trim (' "string\" ', ' "SG '); Final output: \ "String\"
2 Trim (' "string\" ', ' "SG '); Final output: Trin
So the trim () function first removes the whitespace character from the end of the character, filters out the given character to be removed (list), and also applies to the LTrim (), RTrim () function
Definitions and usage
The PHP function trim () deletes white-space characters and other predefined characters from both ends of the string.
Grammar
Trim (str,charlist) parameter 1 str is the string to be manipulated, parameter 2 charlist optional, specifying the special symbol you want to remove.
If the second argument is not given a value, the preset removes the following characters:
"(ASCII (0x20)), an ordinary space.
' \ t ' (ASCII 9 (0x09)), tab.
"\ n" (ASCII 0x0A), a new line (line feed).
' \ R ' (ASCII (0x0D)), a carriage return.
"0x00" (ASCII 0 ()), the Nul-byte.
"\X0B" (ASCII-One (0x0b)), a vertical tab.
If you want to remove other characters, you can set it in the second argument.
Example 1
<?php
$str = "Use function trim to remove whitespace characters from both ends of the string";
$str 1 = Trim ($STR);
echo "Before processing has". strlen ($STR). Characters "; echo "<br/>";
www.phpjc.cn echo "<br/>";
echo "processed with the Trim function". strlen ($str 1). Characters ";
? >
Output:
There are 39 characters before processing with the PHP function trim () 34 characters after processing
Example 2
<?php
$str = "# #使用函数trim去掉字符串两端特定字符";
$str 1 = Trim ($str, "#");
Pass the second argument to the function trim, and
trim deletes the # character Echo $str at both ends of the string $str. <br> ";
echo $str 1;
? >
Output:
# #使用PHP函数trim () remove specific characters at both ends of the string use function trim to remove specific characters at both ends of the string
The above mentioned is the entire content of this article, I hope you can enjoy.