解析php利用正则表达式解决采集内容排版的问题
网络编程 2021-07-05 09:49www.168986.cn编程入门
本篇文章是对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" => '',
"/ /i" => ' ',//空格替换掉
"/<p.?>[ |\x{3000}|\r\n]/ui" => '<p> ',//替换半角、全角空格,换行符,用 排除写入数据库时产生的编码问题
);
$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> '.$content;
$content = tidy_repair_string($content, $config, 'utf8');//再修复一次,可以去除html空标签
$content = trim($content);
return $content;
}
代码如下:
/
格式化内容
@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" => '',
"/ /i" => ' ',//空格替换掉
"/<p.?>[ |\x{3000}|\r\n]/ui" => '<p> ',//替换半角、全角空格,换行符,用 排除写入数据库时产生的编码问题
);
$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> '.$content;
$content = tidy_repair_string($content, $config, 'utf8');//再修复一次,可以去除html空标签
$content = trim($content);
return $content;
}
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指