(PHP 4, PHP 5, PHP 7, PHP 8)
rtrim — 文字列の最後から空白 (もしくはその他の文字) を取り除く
この関数は文字列 string
の最後から空白文字 (あるいはその他の文字) を取り除き、
取り除かれた文字列を返します。
2 番目のパラメータを指定しない場合、 rtrim() は以下の文字を削除します:
" "
: 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
パラメータで指定することもできます。
削除が必要なすべての文字を単純にリストとして記述してください。
..
を使用することで、連続する文字の範囲を指定することも可能です。
変更後の文字列を返します。
例1 rtrim() の使用例
<?php
$text = "\t\tThese are a few words :) ... ";
$binary = "\x09Example string\x0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);
print "\n";
$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " \t.");
var_dump($trimmed);
$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed);
// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive)
$clean = rtrim($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(26) " These are a few words :)" string(9) "Hello Wor" string(15) " Example string"