男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站

trim

(PHP 4, PHP 5, PHP 7, PHP 8)

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

說(shuō)明

trim(string $str, string $character_mask = " \t\n\r\0\x0B"): string

此函數返回字符串 str 去除首尾空白字符后的結果。如果不指定第二個(gè)參數,trim() 將去除這些字符:

  • " " (ASCII 32 (0x20)),普通空格符。
  • "\t" (ASCII 9 (0x09)),制表符。
  • "\n" (ASCII 10 (0x0A)),換行符。
  • "\r" (ASCII 13 (0x0D)),回車(chē)符。
  • "\0" (ASCII 0 (0x00)),空字節符。
  • "\x0B" (ASCII 11 (0x0B)),垂直制表符。

參數

str

待處理的字符串。

character_mask

可選參數,過(guò)濾字符也可由 character_mask 參數指定。一般要列出所有希望過(guò)濾的字符,也可以使用 “..” 列出一個(gè)字符范圍。

返回值

過(guò)濾后的字符串。

范例

示例 #1 trim() 使用范例

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

?>

以上例程會(huì )輸出:

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"

示例 #2 使用 trim() 清理數組值

<?php
function trim_value(&$value

    
$value trim($value); 
}

$fruit = array('apple','banana '' cranberry ');
var_dump($fruit);

array_walk($fruit'trim_value');
var_dump($fruit);

?>

以上例程會(huì )輸出:

array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(7) "banana "
  [2]=>
  string(11) " cranberry "
}
array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(9) "cranberry"
}

注釋

注意: Possible gotcha: removing middle characters

Because trim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim('abc', 'bad') removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas trim('abc', 'b') seemingly does not.

參見(jiàn)

  • ltrim() - 刪除字符串開(kāi)頭的空白字符(或其他字符)
  • rtrim() - 刪除字符串末端的空白字符(或者其他字符)
  • str_replace() - 子字符串替換

男女疯狂一边摸一边做羞羞视频|啊好深好硬快点用力别停动态图|亚洲一区无码中文字幕|特级无码毛片免费视频播放▽|久久狠狠躁免费观看|国内精品久久久久久网站