实用的php清除html,php去除空格与换行,php清除空白行和换行,提取页面纯文本
实用的php清除html,换行,空格类,php去除空格与换行,php清除空白行和换行,提取页面纯文本内容
方法一:
function DeleteHtml($str) { $str = trim($str); //清除字符串两边的空格 $str = preg_replace("//t/","",$str); //使用正则表达式替换内容,如:空格,换行,并将替换为空。 $str = preg_replace("//r/n/","",$str); $str = preg_replace("//r/","",$str); $str = preg_replace("//n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/ /","",$str); //匹配html中的空格 return trim($str); //返回字符串}
调用方法
DeleteHtml($str); $str 为需要清除的页面字符串
方法二:
strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签。
strip_tags() 函数官方介绍: http://www.php.net/manual/zh/function.strip-tags.php
可以加入到以上方法中。
function DeleteHtml($str) { $str = trim($str); //清除字符串两边的空格 $str = strip_tags($str,""); //利用php自带的函数清除html格式 $str = preg_replace("//t/","",$str); //使用正则表达式替换内容,如:空格,换行,并将替换为空。 $str = preg_replace("//r/n/","",$str); $str = preg_replace("//r/","",$str); $str = preg_replace("//n/","",$str); $str = preg_replace("/ /","",$str); $str = preg_replace("/ /","",$str); //匹配html中的空格 return trim($str); //返回字符串}
方法三:
去除字符串内部的空行:
$str = preg_replace("/(s*?r?ns*?)+/","n",$str);
去除全部的空行,包括内部和头尾:
$str = preg_replace('/($s*$)|(^s*^)/m', '',$str);
最后去除全部空格和换行回车
function trimall($str){$qian=array(" "," ","/t","/n","/r");return str_replace($qian, '', $str); }

实用的php清除html,php去除空格与换行,php清除空白行和换行,提取页面纯文本标题图片