解析php利用正则表达式解决采集内容排版的问题
复制代码 代码如下:
", ',
/**
* 格式化内容
* @param string $content 内容最好统一用utf-8编码
* @return string
* !本函数需要开启tidy扩展
*/
function removeFormat($content) {
$replaces = array (
"/
"//i" => '',
"//i" => '',
"//i" => '',
"/
"//i" => '',
"/
"//i" => "
"//i"=>'',
/* "/
"//i" => '',
"/
"//i" => '',
"/
"//i" => '
"/
"/style=.+?['|\"]/i" => '',
"/class=.+?['|\"]/i" => '',
"/id=.+?['|\"]/i"=>'',
"/lang=.+?['|\"]/i"=>'',
//"/width=.+?['|\"]/i"=>'',//不好控制注释掉
//"/height=.+?['|\"]/i"=>'',
"/border=.+?['|\"]/i"=>'',
"/face=.+?['|\"]/i"=>'',
"/
", ',//替换半角、全角空格,换行符,用 排除写入数据库时产生的编码问题 ')>6)//部分内容开头可能缺失 标签 '.$content;
"/
"/ /i" => ' ',//空格替换掉
"/
);
$config = array(
//'indent' => TRUE, //是否缩进
'output-html' => TRUE,//是否是输出xhtml
'show-body-only'=>TRUE,//是否只获得到body
'wrap' => 0
);
$content = tidy_repair_string($content, $config, 'utf8');//先利用php自带的tidy类库修复html标签,不然替换的时候容易出现各种诡异的情况
$content = trim($content);
foreach ( $replaces as $k => $v ) {
$content = preg_replace ( $k, $v, $content );
}
if(strpos($content,'
$content = '
$content = tidy_repair_string($content, $config, 'utf8');//再修复一次,可以去除html空标签
$content = trim($content);
return $content;
}