去除字符串首尾空白等特殊符号或指定字符序列。
语法:
string trim(string str[, charlist])
当设定字符序列 charlist 参数时,trim() 函数将去除字符串首尾的这些字符,否则 trim() 函数将去除字符串首尾的以下这些特殊字符:
| 字符 | 说明 |
|---|---|
| 空格 | |
| t | tab键 |
| n | 换行符 |
| r | a carriage return |
| 空字符 | |
| x0B | a vertical tab |
例子:
<?php $text = "Hello World "; $trimmed = trim($text); echo $trimmed; //输出"Hello World" echo "<br />"; echo trim($trimmed, "Hdle"); //输出"o Wor" echo "<br />"; echo trim($text, "Hdle"); //输出"o World" ?>
从这个例子可以看出,trim() 函数将不会去除非首尾的 charlist 。