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 参数。 | 
更多实例
实例
移除字符串右侧的空格:
<?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!
实例
移除字符串右侧的换行符(\n):
<?php
$str = "Hello World!\n\n\n";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
$str = "Hello World!\n\n\n";
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!
