删除字符串两侧的字符("Hello" 中的 "He" 和 "World" 中的 "d!"):
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
亲自试一试 »
Trim() 函数从字符串两侧删除空格和其他预定义字符。
相关功能:
trim(
string,charlist)
Parameter | Description |
---|---|
string | Required. Specifies the string to check |
charlist | Optional. Specifies which characters to remove from the string. If omitted, all of the following characters are removed:
|
返回值: | 返回修改后的字符串 |
---|---|
PHP 版本: | 4+ |
变更日志: | 这个字符列表PHP 4.1 中添加了参数 |
删除字符串两侧的空格:
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
上述代码的 HTML 输出将是(查看源代码):
<!DOCTYPE html>
<html>
<body>
Without trim: Hello World! <br>With trim: Hello World!
</body>
</html>
上述代码的浏览器输出将是:
Without trim: Hello World!
With trim: Hello World!
亲自试一试 »
从字符串两侧删除换行符 (\n):
<?php
$str = "\n\n\nHello World!\n\n\n";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
上述代码的 HTML 输出将是(查看源代码):
<!DOCTYPE html>
<html>
<body>
Without trim:
Hello World!
<br>With trim: Hello World!
</body>
</html>
上述代码的浏览器输出将是:
Without trim: Hello World!
With trim: Hello World!
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!