php trim 删除空格字符与指定字符
以前没看trim函数以为他只能删除空白,今天发现它还可以删除"�" - null
"t" - tab
"n" - new line
"x0b" - 纵向列表符
"r" - 回车
" " - 普通空白字符
以及用户指定字符哦。
php教程 定义和用法 trim() 函数从字符串的两端删除空白字符和其他预定义字符。 可选。规定要转换的字符串。如果省略该参数,则删除以下所有字符: "�" - null "t" - tab "n" - new line "x0b" - 纵向列表符 "r" - 回车 " " - 普通空白字符 $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 " {$str} "; // $str = "##使用函数trim去掉字符串两端特定字符####"; $str1 = trim($str,"#"); //为函数trim传入第二个参数,trim将删除字符串$str两端的#字符 echo $str." "; echo $str1; ?> ?>
永久链接:http://www.phprm.com/function/php1004603.html
转载随意!带上文章地址吧。