ltrim
(PHP 4, PHP 5, PHP 7, PHP 8)
ltrim — 刪除字串開頭的空白字元(或其他字元)
說明
ltrim(string
$str, string $character_mask = ?): string刪除字串開頭的空白字元(或其他字元)
參數
-
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)),NUL空位元組符。 -
"\x0B" (ASCII
11(0x0B)),垂直製表符。
範例
示例 #1 ltrim()的使用範例
<?php
$text = "\t\tThese are a few words :) ... ";
$binary = "\x09Example string\x0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);
print "\n";
$trimmed = ltrim($text);
var_dump($trimmed);
$trimmed = ltrim($text, " \t.");
var_dump($trimmed);
$trimmed = ltrim($hello, "Hdle");
var_dump($trimmed);
// 刪除 $binary 開頭的 ASCII 控制字元
// (從 0 到 31,包括 0 和 31)
$clean = ltrim($binary, "\x00..\x1F");
var_dump($clean);
?>
以上例程會輸出:
string(32) " These are a few words :) ... " string(16) " Example string " string(11) "Hello World" string(30) "These are a few words :) ... " string(30) "These are a few words :) ... " string(7) "o World" string(15) "Example string "