php trim函数用法实例

发布时间:2019-12-18编辑:脚本学堂
本文介绍了php trim函数的用法,trim函数的定义与例子,有需要的朋友参考下。

php去空格函数trim的定义和用法。

trim() 函数从字符串的两端删除空白字符和其他预定义字符。
语法trim(string,charlist)string必需。规定要检查的字符串。
charlist 可选,规定要转换的字符串。如果省略该参数,则删除以下所有字符:
 

"" - null
"t" - tab
"n" - new line
"x0b" - 纵向列表符
"r" - 回车
" " - 普通空白字符

例子:
 

复制代码 代码示例:
<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 = "rnhello world!rn";echo "without trim: " . $str;echo "<br />";echo "with trim: " . trim($str);?>
输出:
without trim: hello world!with trim: hello world!

例子:
 

复制代码 代码示例:
<html><body>without trim: hello world!<br />with trim: hello world!</body></html> 444<?php
//$c = include "2.php";
//echo $c[0];
$str = " this is a string <br>kwga";
echo $str;
echo "<p>";
$newstr = trim($str,"t,g,r,t, ,");
echo $newstr;
?>