PHP字符串函數之 trim ltrim rtrim chop
- trim – 去除字符串首尾處的空白字符(或者其他字符)
- ltrim – 刪除字符串開頭的空白字符(或其他字符)
- rtrim – 刪除字符串末端的空白字符(或者其他字符)
- chop – rtrim() 的別名
trim
去除字符串首尾處的空白字符(或者其他字符)
string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )
參數說明
str
待處理的字符串。
charlist
可選參數,過濾字符也可由 charlist 參數指定。一般要列出所有希望過濾的字符,也可以使用 “..” 列出一個字符範圍。
返回值
過濾後的字符串。
注意
- 該函數區分大小寫
- 此函數返回字符串 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 = " There are a few words ";
var_dump($text); //string ' There are a few words ' (length=23)
var_dump(trim($text)); //string 'There are a few words' (length=21)
/* 去製表符 */
$text = "\tThere are a few words\t";
var_dump($text); //string ' There are a few words ' (length=25)
var_dump(trim($text)); //string 'There are a few words' (length=21)
/* 去換行符 */
$text = "\nThere are a few words\n";
var_dump($text);
/*string '
There are a few words
' (length=23) */
var_dump(trim($text)); //string 'There are a few words' (length=21)
/* 去回車符 */
$text = "\rThere are a few words\r";
var_dump($text);
/*string '
There are a few words
' (length=23) */
var_dump(trim($text)); //string 'There are a few words' (length=21)
/* 去空字節符 */
$text = "\0There are a few words\0";
var_dump($text); //string '�There are a few words�' (length=23)
var_dump(trim($text));//string 'There are a few words' (length=21)
/* 去垂直製表符 */
$text = "\x0BThere are a few words\x0B";
var_dump($text); //string ' There are a few words ' (length=23)
var_dump(trim($text));//string 'There are a few words' (length=21)
/* 各種去 */
$text = "\x0B\r\n\t\0 There are a few words \x0B\r\n\t\0";
var_dump($text);
/*string '
� There are a few words
�' (length=33)*/
var_dump(trim($text)); //string 'There are a few words' (length=21)
?>
<?php
/* 去掉指定的字符 */
$text = "There are a few words";
$text = trim($text, 's');
var_dump($text); //string 'There are a few word' (length=20)
/* 去掉指定的字符串 */
$text = "There are a few words";
$text = trim($text, 'words');
var_dump($text); //string 'There are a few ' (length=16)
/* 第二個參數爲數字是無效的 */
$text = "There are a few words";
$text = trim($text, 115);
var_dump($text); //string 'There are a few words' (length=21)
/* 第二個參數爲ASCII碼 (s的ASCII爲 115(0x73)) */
$text = "There are a few words";
$text = trim($text, "\x73");
var_dump($text); //string 'There are a few word' (length=20)
/* 去掉 0-31 ASCII控制字符 */
$text = "\x01\x02There are a few words\x0A\x1F";
$text = trim($text, "\x00..\x1F");
var_dump($text); //string 'There are a few words' (length=21)
?>
ltrim
刪除字符串開頭的空白字符(或其他字符)
string ltrim ( string $str [, string $character_mask ] )
該函數與trim的區別是僅僅去除左側的空白字符。示例可以參考trim
參數說明
str
待處理的字符串。
character_mask
可選參數,過濾字符也可由 character_mask 參數指定。一般要列出所有希望過濾的字符,也可以使用 “..” 列出一個字符範圍。
返回值
過濾後的字符串。
注意
- 該函數區分大小寫
- 該函數返回一個刪除了 str 最左邊的空白字符的字符串。 如果不使用第二個參數, ltrim() 僅刪除以下字符:
" " (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 = "There are a few words";
$text = ltrim($text, "T");
var_dump($text); //string 'here are a few words' (length=20)
$text = "There are a few words";
$text = ltrim($text, "s");
var_dump($text); //string 'There are a few words' (length=21)
?>
rtrim
刪除字符串末端的空白字符(或者其他字符)
string rtrim ( string $str [, string $character_mask ] )
該函數與trim的區別是僅僅去除右側的空白字符。示例可以參考trim
參數說明
str
待處理的字符串。
character_mask
可選參數,過濾字符也可由 character_mask 參數指定。一般要列出所有希望過濾的字符,也可以使用 “..” 列出一個字符範圍。
返回值
過濾後的字符串。
注意
- 該函數區分大小寫
- 該函數返回一個刪除了 str 最右邊的空白字符的字符串。 如果不使用第二個參數, rtrim() 僅刪除以下字符:
" " (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 = "There are a few words";
$text = rtrim($text, "T");
var_dump($text); //string 'There are a few words' (length=21)
$text = "There are a few words";
$text = rtrim($text, "s");
var_dump($text); //string 'There are a few word' (length=20)
?>
chop
刪除字符串末端的空白字符(或者其他字符)
string chop ( string $str [, string $character_mask ] )
該函數是 rtrim()
的別名。可參考rtrim()
參數說明
str
待處理的字符串。
character_mask
可選參數,過濾字符也可由 character_mask 參數指定。一般要列出所有希望過濾的字符,也可以使用 “..” 列出一個字符範圍。
返回值
過濾後的字符串。
注意
- 該函數區分大小寫
- 該函數返回一個刪除了 str 最右邊的空白字符的字符串。 如果不使用第二個參數, chop() 僅刪除以下字符:
" " (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 = "There are a few words";
$text = chop($text, "T");
var_dump($text); //string 'There are a few words' (length=21)
$text = "There are a few words";
$text = chop($text, "s");
var_dump($text); //string 'There are a few word' (length=20)
?>