Instance
Remove the character to the left of the string ("He" in "Hello" and "d!" in "World") ):
<?php$str = "Hello world!"; Echo $str. "<br>"; echo Trim ($str, "hed!"); >
Definition and usage
The trim () function removes white space characters or other predefined characters on either side of the string.
Related functions:
Grammar
Trim (string,charlist)
parameters |
description |
string |
required. Specifies the string to check. |
charlist |
optional. Specifies which characters are removed from the string. If this argument is omitted, all of the following characters are removed:
-
" \ "-NULL
-
"\ T"-Tabs
-
"\ n"-line break
-
"\x0b"-vertical tab
-
"\ r"-Enter
-
""-Space
|
Technical details
return value: |
Returns the modified string. |
PHP version: |
4 + |
Update log: |
In PHP 4.1, the charlist parameter has been added. |
More examples
Example 1
To remove spaces on either side of a string:
<?php$str = "Hello world!"; echo "without Trim:". $str, echo "<br>", echo "with Trim:". Trim ($STR);? >
The HTML output of the above code is as follows (see source code):
<! DOCTYPE html>
The browser output of the above code is as follows:
Without Trim:hello world! With Trim:hello world!
Example 2
Remove newline characters (\ n) on both sides of the string:
<?php$str = "Nnnhello world!nnn"; echo "without Trim:". $str, echo "<br>", echo "with Trim:". Trim ($STR);? >
The HTML output of the above code is as follows (see source code):
<! DOCTYPE html>
The browser output of the above code is as follows:
Without Trim:hello world! With Trim:hello world!
Example
<?php $str = "# #使用函数trim去掉字符串两端特定字符 # # #; $str 1 = Trim ($str, "#"); When you pass in the second parameter for function trim, Trim removes 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 a string # # # # Use function trim to remove specific characters at both ends