PHP rtrim() 函式
定義和用法
rtrim() 函式用於從字串的右側移除空格或其他預定義字元。
相關函式
語法
rtrim(string,charlist)
引數值
引數 | 描述 |
---|---|
string | 必需。指定要檢查的字串 |
charlist | 可選。指定要從字串中移除的字元。如果省略,則移除所有以下字元
|
技術詳情
返回值 | 返回修改後的字串 |
---|---|
PHP 版本 | 4+ |
更新日誌 | charlist 引數在 PHP 4.1 中新增 |
更多示例
示例
從字串的右側移除空格
<?php
$str = "Hello World! ";
echo "不使用 rtrim: " . $str;
echo "<br>";
echo "使用 rtrim: " . rtrim($str);
?>
上述程式碼的 HTML 輸出是 (檢視原始碼)
<!DOCTYPE html>
<html>
<body>
不使用 rtrim: Hello World! <br>使用 rtrim: Hello World!
</body>
</html>
上述程式碼的瀏覽器輸出是
不使用 rtrim: Hello World!
使用 rtrim: Hello World!
自己動手試一試 »
示例
從字串的右側移除換行符 (\n)
<?php
$str = "Hello World!\n\n\n";
echo "不使用 rtrim: " . $str;
echo "<br>";
echo "使用 rtrim: " . rtrim($str);
?>
上述程式碼的 HTML 輸出是 (檢視原始碼)
<!DOCTYPE html>
<html>
<body>
不使用 rtrim: Hello World!
<br>使用 rtrim: Hello World!
</body>
</html>
上述程式碼的瀏覽器輸出是
不使用 rtrim: Hello World!
使用 rtrim: Hello World!
自己動手試一試 »
❮ PHP 字串參考