Home > php教程 > php手册 > body text

php中的实现trim函数代码

WBOY
Release: 2016-06-13 12:32:41
Original
1085 people have browsed it

去掉前后的空格. 

假设有一个字符串" ddd dd d ",经过Trim()之后成为"ddd dd d". 

如上可以去掉两边多余的空格(包括制表符),但不能去掉中间的空格. 
PHP中的trim()函数跟ASP中的trim()函数用法一样的,如果你接触过ASP,那么就自然会用的. 
用法比较简单,在想要去掉空格的变量中加入如下: 
echo trim($变量); 
?>

一般用到用户的password处理中。

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

语法
trim(string,charlist)参数 描述
string 必需。规定要检查的字符串。
charlist 可选。规定要转换的字符串。如果省略该参数,则删除以下所有字符:
"\0" - NULL
"\t" - tab
"\n" - new line
"\x0B" - 纵向列表符
"\r" - 回车
" " - 普通空白字符
例子 1

复制代码 代码如下:




$str = " Hello World! ";
echo "Without trim: " . $str;
echo "
";
echo "With trim: " . trim($str);
?>



输出:

复制代码 代码如下:


Without trim: Hello World!
With trim: Hello World!HTML 源码:



例子 2

复制代码 代码如下:


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


输出:

Without trim: Hello World!
With trim: Hello World!HTML
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template