Home > Backend Development > PHP Tutorial > php过滤所有的空白字符(空格、全角空格、换行等)_php技巧

php过滤所有的空白字符(空格、全角空格、换行等)_php技巧

WBOY
Release: 2016-05-16 20:05:59
Original
2309 people have browsed it

在php中自带的trim函数只能替换左右两端的空格,感觉在有些情况下不怎么好使,如果要将一个字符串中所有空白字符过滤掉(空格、全角空格、换行等),那么我们可以自己写一个过滤函数。

php学习str_replace函数都知道,可以批量替换的,所以我们可以用如下的源码实现替换过滤一个字符串所有空白字符了。

<&#63;php
$str = 'jkgsd
gsgsdgs gsdg gsd';
echo myTrim($str);
function myTrim($str)
{
 $search = array(" "," ","\n","\r","\t");
 $replace = array("","","","","");
 return str_replace($search, $replace, $str);
}
&#63;>

Copy after login

运行代码,页面输出:jkgsdgsgsdgsgsdggsd,完美实现了我们想要的效果。

以上就是php过滤所有的空白字符的方法,希望对大家的学习有所帮助。

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template