PHP rtrim()関数
例
文字列の右側の文字を削除する:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
例を実行 »
定義と使用法
rtrim() 関数は、文字列の右側の空白文字またはその他の定義済み文字を削除します。
関連関数:
構文
rtrim(string,charlist)
| パラメータ | 説明 |
|---|---|
| string | 必須。検査する文字列を指定します。 |
| charlist | 省略可能。文字列から削除する文字を指定します。このパラメータを省略した場合、次のすべての文字が削除されます:
|
技術詳細
| 戻り値: | 修正された文字列を返します。 |
|---|---|
| PHP バージョン: | 4+ |
| 更新ログ: | PHP 4.1 で、次のものが追加されました:charlistパラメータ。 |
その他の例
例 1
文字列の右側のスペースを削除する:
<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
上記コードの HTML 出力は次のとおりです(ソースコードを表示):
<!DOCTYPE html>
<html>
<body>
Without rtrim: Hello World! <br>With rtrim: Hello World!
</body>
</html>
<html>
<body>
Without rtrim: Hello World! <br>With rtrim: Hello World!
</body>
</html>
上記コードのブラウザ出力は次のとおりです:
Without rtrim: Hello World!
With rtrim: Hello World!
With rtrim: Hello World!
例を実行 »
例 2
文字列の右側の改行文字(\n)を削除する:
<?php
$str = "Hello World!nnn";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
$str = "Hello World!nnn";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
上記コードの HTML 出力は次のとおりです(ソースコードを表示):
<!DOCTYPE html>
<html>
<body>
Without rtrim: Hello World!
<br>With rtrim: Hello World!
</body>
</html>
<html>
<body>
Without rtrim: Hello World!
<br>With rtrim: Hello World!
</body>
</html>
上記コードのブラウザ出力は次のとおりです:
Without rtrim: Hello World!
With rtrim: Hello World!
With rtrim: Hello World!
例を実行 »
PHP String リファレンスマニュアル その他の拡張