(PHP 4, PHP 5, PHP 7, PHP 8)
ltrim — 文字列の最初から空白 (もしくはその他の文字) を取り除く
文字列の最初から空白 (もしくはその他の文字) を取り除きます。
二番目の引数を指定しない場合、 mb_ltrim() は以下の文字を取り除きます:
" "
: ASCII の SP 文字
0x20
、通常のスペース。
"\t"
: ASCII の HT 文字
0x09
、タブ。
"\n"
: ASCII の LF 文字
0x0A
、改行(ラインフィード)。
"\r"
: ASCII の CR 文字
0x0D
、キャリッジリターン。
"\0"
: ASCII の NUL 文字
0x00
、NULバイト。
"\v"
: ASCII VT 文字
0x0B
、垂直タブ。
string
characters
characters
パラメータで指定することもできます。
削除が必要なすべての文字を単純にリストとして記述してください。
..
を使用することで、連続する文字の範囲を指定することも可能です。
この関数は、string
の先頭から空白文字を削除して返します
例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);
// trim the ASCII control characters at the beginning of $binary
// (from 0 to 31 inclusive)
$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 "