php利用正则表达式解决采集内容排版问题

做采集经常遇到的问题是内容排版问题,用了一些时间写了个用正则替换html标签和样式的函数,共享下。

/**
 * 格式化内容
 * @param string $content 内容最好统一用utf-8编码
 * @return string
 * !本函数需要开启tidy扩展
 */
function removeFormat($content) {
	$replaces = array (
			"/<font.*?>/i" => '',
			"/<\/font>/i" => '',
			"/<strong>/i" => '',
			"/<\/strong>/i" => '',
			"/<span.*?>/i" => '',
			"/<\/span>/i" => '',
			"/<div.*?>/i" => "<p>",
			"/<\/div>/i" => "</p>",
			"/<!--<.*?>*-->/i"=>'',
			/* "/<table.*?>/i" => '',//遇到有表格的内容就不要启用
			"/<\/table>/i" => '',
			"/<tbody.*?>/i" => '',
			"/<\/tbody>/i" => '',
			"/<tr.*?>/i" => '<p>',
			"/<\/tr>/i" => '</p>',
			"/<td.*?>/i" => '', */
			"/style=.+?['|\"]/i" => '',
			"/class=.+?['|\"]/i" => '',
			"/id=.+?['|\"]/i"=>'',
			"/lang=.+?['|\"]/i"=>'',
			//"/width=.+?['|\"]/i"=>'',//不好控制注释掉
			//"/height=.+?['|\"]/i"=>'',
			"/border=.+?['|\"]/i"=>'',
			"/face=.+?['|\"]/i"=>'',
			"/<br.*?>[ ]*/i" => "</p><p>",
			"/<iframe.*?>.*<\/iframe>/i" => '',
			"/&nbsp;/i" => ' ',//空格替换掉
			"/<p.*?>[ |\x{3000}|\r\n]*/ui" => '<p>&nbsp;&nbsp;&nbsp;&nbsp;',//替换半角、全角空格,换行符,用&nbsp;排除写入数据库时产生的编码问题
			 
	);
	$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,'<p>')>6)//部分内容开头可能缺失<p>标签
		$content = '<p>&nbsp;&nbsp;&nbsp;&nbsp;'.$content;
		
	$content = tidy_repair_string($content, $config, 'utf8');//再修复一次,可以去除html空标签
	$content = trim($content);
	return $content;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值