PHP字符串函数之 trim ltrim rtrim chop


#1

#PHP字符串函数之 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, 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 参数指定。一般要列出所有希望过滤的字符,也可以使用 “…” 列出一个字符范围。
####返回值
过滤后的字符串。
####注意

  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)
?>