分享两个PHP去除空格空行HTML、JS、CSS代码的函数
这个函数是先用php自带的strip_tag函数过滤掉html代码,在用正则表达式匹配空格并替换
- public function html_remove($str){
- $htmlstr=strip_tags(htmlspecialchars_decode($str)); //这里是对实体符号进行了转义,如果你不需要可以去掉htmlspecialchars_decode函数
- return preg_replace(“/(\s|\ \;| |\xc2\xa0)/”,“”,$htmlstr);
- }
这个函数可以过滤掉传入参数中的Html代码、Javascript代码、css代码、回车、换行、制表位以及空格
- function remove_html_tag($str){ //清除HTML代码、空格、回车换行符
- $str = trim($str); //trim 去掉字串两端的空格
- $str = @preg_replace(‘/<script[^>]*?>(.*?)<\/script>/si’, ”, $str); //去除js代码
- $str = @preg_replace(‘/<style[^>]*?>(.*?)<\/style>/si’, ”, $str); //去除css代码
- $str = @strip_tags($str,“”); //strip_tags 删除HTML元素
- $str = @ereg_replace(“\t”,“”,$str); //删除制表位
- $str = @ereg_replace(“\r\n”,“”,$str); //删除回车换行
- $str = @ereg_replace(“\r”,“”,$str); //删除回车
- $str = @ereg_replace(“\n”,“”,$str); //删除换行
- $str = @ereg_replace(” “,“”,$str); //把连接的多个空格替换成单空格
- $str = @ereg_replace(“ ”,“”,$str); //替换空格
- return trim($str);
- }