Home>Article>Backend Development> PHP function trim() that removes whitespace characters or other predefined characters on both sides of a string
Example
Remove the characters on the left side ofString("He" in "Hello" and "d!" in "World"):
"; echo trim($str,"Hed!"); ?>
Definition and Usage
trim() function removes whitespace characters or other predefined characters on both sides of a string.
Related functions:
ltrim() - Removes whitespace characters or other predefined characters on the left side of a string.
rtrim() - Removes whitespace characters or other predefined characters on the right side of the string.
Syntax
trim(string,charlist)
Parameters | Description |
string | Required. Specifies the string to check. |
charlist | Optional. Specifies which charactersare removed from the string. If this parameter is omitted, all the following characters are removed:
|
PHP Version: | |
##Update Log | |
In PHP 4.1, the charlist parameter was added. | 更多实例 实例 1 移除字符串两侧的空格: "; echo "With trim: " . trim($str); ?> 上面代码的 HTML 输出如下(查看源代码): Without trim: Hello World! 上面代码的浏览器输出如下: Without trim: Hello World! With trim: Hello World! 实例 2 移除字符串两侧的换行符(\n): "; echo "With trim: " . trim($str); ?> 上面代码的 HTML 输出如下(查看源代码): Without trim: Hello World! 上面代码的浏览器输出如下: Without trim: Hello World! With trim: Hello World! 例子 "; echo $str1; ?> 输出: ##使用PHP函数trim()去掉字符串两端特定字符#### 使用函数trim去掉字符串两端特定字符 |
The above is the detailed content of PHP function trim() that removes whitespace characters or other predefined characters on both sides of a string. For more information, please follow other related articles on the PHP Chinese website!