[php]trim — 去除字符串首尾處的空白字符(或者其他字符)


此函數返回字符串 str 去除首尾空白字符後的結果。如果不指定第二個參數,trim() 將去除這些字符:
" " (ASCII 32 (0x20)),普通空格符。
"\t" (ASCII 9 (0x09)),製表符。
"\n" (ASCII 10 (0x0A)),換行符。
"\r" (ASCII 13 (0x0D)),回車符。
"\0" (ASCII 0 (0x00)),空字節符。
"\x0B" (ASCII 11 (0x0B)),垂直製表符。


官方範例


<?php


$text   = "\t\tThese are a few words :) ...  ";
$binary = "\x09Example string\x0A";
$hello  = "Hello World";
var_dump($text, $binary, $hello);


print "\n";


$trimmed = trim($text);
var_dump($trimmed);


$trimmed = trim($text, " \t.");
var_dump($trimmed);


$trimmed = trim($hello, "Hdle");
var_dump($trimmed);


// 清除 $binary 首位的 ASCII 控制字符
// (包括 0-31)
$clean = trim($binary, "\x00..\x1F");
var_dump($clean);


?>
以上例程會輸出:
string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"


string(28) "These are a few words :) ..."
string(24) "These are a few words :)"
string(5) "o Wor"
string(14) "Example string"

arrow
arrow
    全站熱搜

    stockwfj3 發表在 痞客邦 留言(0) 人氣()