当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > trim() 函数

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 trim() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 578 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The trim() function removes whitespaces and other predefined characters from both sides of a string.
trim()函数的作用是:去除字符串首末尾的空白(或其它字符)。

Syntax
语法

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:
可选参数。指定需要从指定的字符串中删掉那些字符。如果忽略该参数,那么下列所有的字符将都被删除:
  • "" - NULL
    "" – NULL(空值)
  • "t" - tab
    "t" – tab(制表符)
  • "n" - new line
    "n" –new line(新行)
  • "x0B" - vertical tab
    "x0B" – vertical tab(垂直制表符)
  • "r" - carriage return
    "r" –carriage return(回车)
  • " " - ordinary white space
    " " –ordinary white space(空格)


Example 1
案例1

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

The browser output of the code above will be:
上述代码将输出下面的结果:

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

If you select "View source" in the browser window, you will see the following HTML:
如果你在浏览器中的选择“查看源文件”,你将看到下面的HTML代码:

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


Example 2
案例2

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

The browser output of the code above will be:
上述代码将输出下面的结果:

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

If you select "View source" in the browser window, you will see the following HTML:
如果你在浏览器中的选择“查看源文件”,你将看到下面的HTML代码:

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

评论 (0) All

登陆 | 还没注册?