您现在的位置是:首页 > PHP函数

李清波 2021-06-17 PHP函数 1633 复制当前网址

trim



PHP trim() 函数

定义和用法

trim() 函数从字符串的两端删除空白字符和其他预定义字符。


语法

trim(string,charlist)


参数描述
string必需。规定要检查的字符串。
charlist可选。规定要转换的字符串。如果省略该参数,则删除以下所有字符:
  • "\0" - NULL

  • "\t" - tab

  • "\n" - new line

  • "\x0B" - 纵向列表符

  • "\r" - 回车

  • " " - 普通空白字符


例子

例子 1

<html>
<body>
<?php
$str = "    Hello World!    ";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " . trim($str);
?>
<body>
<html>

输出:

Without trim: Hello World!
With trim: Hello World!

HTML 源码:

<html>
<body>
Without trim:     Hello World!    <br />With trim: Hello World!
</body>
</html>


例子 2

<?php
$str = "\r\nHello World!\r\n";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " . trim($str);
?>

输出:

Without trim: Hello World!
With trim: Hello World!

HTML 源码:

<html>
<body>
Without trim: 
Hello World!
<br />With trim: Hello World!
</body>
</html>


文章来源:http://liqingbo.com/blog-885.html

上一篇:ucfirst

下一篇:substr_replace

评论