天天看點

PHP字元串函數之 trim ltrim rtrim chopPHP字元串函數之 trim ltrim rtrim chop

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 參數指定。一般要列出所有希望過濾的字元,也可以使用 “..” 列出一個字元範圍。

傳回值

過濾後的字元串。

注意

  1. 該函數區分大小寫
  2. 此函數傳回字元串 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 '&#0;There are a few words&#0;' (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 '
    &#0; There are a few words     
    &#0;' (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, );
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 參數指定。一般要列出所有希望過濾的字元,也可以使用 “..” 列出一個字元範圍。

傳回值

過濾後的字元串。

注意

  1. 該函數區分大小寫
  2. 該函數傳回一個删除了 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 參數指定。一般要列出所有希望過濾的字元,也可以使用 “..” 列出一個字元範圍。

傳回值

過濾後的字元串。

注意

  1. 該函數區分大小寫
  2. 該函數傳回一個删除了 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 參數指定。一般要列出所有希望過濾的字元,也可以使用 “..” 列出一個字元範圍。

傳回值

過濾後的字元串。

注意

  1. 該函數區分大小寫
  2. 該函數傳回一個删除了 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)
?>