php trim 删除空格字符与指定字符
以前没看trim函数以为他只能删除空白,今天发现它还可以删除"" - null.
"t" - tab ,"n" - new line ,"x0b" - 纵向列表符 ,"r" - 回车 ," " - 普通空白字符,以及用户指定字符.
定义和用法:trim() 函数从字符串的两端删除空白字符和其他预定义字符,可选,规定要转换的字符串,如果省略该参数,则删除以下所有字符:
"" - null,"t" - tab,"n" - new line,"x0b" - 纵向列表符,"r" - 回车," " - 普通空白字符,实例代码如下:
<?php $str = " this line containstliberal rn use of whitespace.nn"; // first remove the leading/trailing whitespace //去掉开始和结束的空白 $str = trim($str); // now remove any doubled-up whitespace //去掉跟随别的挤在一块的空白 $str = preg_replace('/s(?=s)/', '', $str); // finally, replace any non-space whitespace, with a space //最后,去掉非space 的空白,用一个空格代替 $str = preg_replace('/[nrt]/', ' ', $str); echo "<pre>{$str}</pre>"; // <?php $str = "##使用函数trim去掉字符串两端特定字符####"; $str1 = trim($str,"#"); //为函数trim传入第二个参数,trim将删除字符串$str两端的#字符 echo $str."<br>"; echo $str1;
文章链接:http://www.phprm.com/develop/fs5649.html
随便收藏,请保留本文地址!