PHP中trim()函數簡單使用指南
string trim ( string $str [, string $charlist ] ) - 去除字串首尾處的空白字元(或者其他字元)
trim()函數當第二個參數為空白時,預設去掉空格、定位字元、分行符號、斷行符號符、垂直定位字元等,當加入第二個參數時
代碼如下:
1) trim(' \"string\"', '\"sg'); // 最終輸出:\"strin
2) trim(' \"string\" ', '\"sg'); // 最終輸出:\"string\"
2)trim('\"string\"', '\"sg'); // 最終輸出:trin
所以trim()函數優先去掉字元首尾的空白字元,再過濾掉給定的要去除的字元(列表),也適用於ltrim()、rtrim()函數
定義和用法
PHP函數trim()從字串的兩端刪除空白字元和其他預定義字元。
文法
trim(str,charlist)參數 1 str為待操作的字串,參數 2 charlist 可選,指定了想要去除的特殊符號。
如果第二個參數沒給值,預設會去除下列這些字元:
" " (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.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab.
如果要去除其它字元,可以在第二個參數裡設定。
例子1
|
1
2
3
4
5
6
7 |
$str = " 使用函數trim去掉字串兩端空白字元 ";
$str1 = trim($str);
echo "處理前有".strlen($str)."個字元"; echo "
";
//www.phpjc.cn echo "
";
echo "使用trim函數處理後有".strlen($str1)."個字元";
?>
|
輸出:
處理前有39個字元 使用PHP函數trim()處理後有34個字元
例子2
|
1
2
3
4
5
6
7 |
$str = "##使用函數trim去掉字串兩端特定字元####";
$str1 = trim($str,"#");
//為函數trim傳入第二個參數,
trim將刪除字串$str兩端的#字元 echo $str."
";
echo $str1;
?>
|
輸出:
##使用PHP函數trim()去掉字串兩端特定字元#### 使用函數trim去掉字串兩端特定字元
http://www.bkjia.com/PHPjc/985140.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/985140.htmlTechArticlePHP中trim()函數簡單使用指南 string trim ( string $str [, string $charlist ] ) - 去除字串首尾處的空白字元(或者其他字元) trim()函數當第二個參數...