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!
