亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解析php利用正則表達(dá)式解決采集內(nèi)容排版的問題

 更新時(shí)間:2013年06月20日 11:44:04   作者:  
本篇文章是對(duì)php利用正則表達(dá)式解決采集內(nèi)容排版問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
做采集經(jīng)常遇到的問題是內(nèi)容排版問題,用了一些時(shí)間寫了個(gè)用正則替換html標(biāo)簽和樣式的函數(shù),共享下。
復(fù)制代碼 代碼如下:

/**
 * 格式化內(nèi)容
 * @param string $content 內(nèi)容最好統(tǒng)一用utf-8編碼
 * @return string
 * !本函數(shù)需要開啟tidy擴(kuò)展
 */
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" => '',//遇到有表格的內(nè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;排除寫入數(shù)據(jù)庫時(shí)產(chǎn)生的編碼問題

 );
 $config = array(
         //'indent' => TRUE, //是否縮進(jìn) 
                'output-html' => TRUE,//是否是輸出xhtml 
                'show-body-only'=>TRUE,//是否只獲得到body 
               'wrap' => 0
    );
 $content = tidy_repair_string($content, $config, 'utf8');//先利用php自帶的tidy類庫修復(fù)html標(biāo)簽,不然替換的時(shí)候容易出現(xiàn)各種詭異的情況
 $content = trim($content);
 foreach ( $replaces as $k => $v ) {
  $content = preg_replace ( $k, $v, $content );
 }

 if(strpos($content,'<p>')>6)//部分內(nèi)容開頭可能缺失<p>標(biāo)簽
  $content = '<p>&nbsp;&nbsp;&nbsp;&nbsp;'.$content;

 $content = tidy_repair_string($content, $config, 'utf8');//再修復(fù)一次,可以去除html空標(biāo)簽
 $content = trim($content);
 return $content;
}

相關(guān)文章

最新評(píng)論