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の文字列リファレンス