PHP將HTML轉換成文本一些方法總結

來源:互聯網
上載者:User

最常用的使用php函數strip_tags

 代碼如下 複製代碼


<?php
$mystr=<<<SATO
此處省略幾十行HTML代碼^_^
SATO;
$str=strip_tags($mystr);
//到這裡就已經達到我的HTML轉為TXT文本的目的了,哈哈,使用這個函數真方便
//下面是外掛程式的一些切詞等操作,這裡就不多說了
?>


自訂函數

 代碼如下 複製代碼

<?php
// $document 應包含一個 HTML 文檔。
// 本例將去掉 HTML 標籤,javascript 代碼
// 和空白字元。還會將一些通用的
// HTML 實體轉換成相應的文本。

$search = array ("'<script[^>]*?>.*?</script>'si",  // 去掉 javascript
                 "'<[/!]*?[^<>]*?>'si",           // 去掉 HTML 標籤
                 "'([rn])[s]+'",                 // 去掉空白字元
                 "'&(quot|#34);'i",                 // 替換 HTML 實體
                 "'&(amp|#38);'i",
                 "'&(lt|#60);'i",
                 "'&(gt|#62);'i",
                 "'&(nbsp|#160);'i",
                 "'&(iexcl|#161);'i",
                 "'&(cent|#162);'i",
                 "'&(pound|#163);'i",
                 "'&(copy|#169);'i",
                 "'&#(d+);'e");                    // 作為 PHP 代碼運行

$replace = array ("",
                  "",
                  "\1",
                  """,
                  "&",
                  "<",
                  ">",
                  " ",
                  chr(161),
                  chr(162),
                  chr(163),
                  chr(169),
                  "chr(\1)");

$text = preg_replace ($search, $replace, $document);
?>

後來我從網上看到了一個使用PHP寫的方法,使用這個方法也可以實現將HTML轉為TXT文本,個人覺得也還蠻實用的,在這裡分享一下,代碼如下:

 代碼如下 複製代碼
function HtmlToText($str){
  $str=preg_replace("/<sty(.*)/style>|<scr(.*)/script>|<!--(.*)-->/isU","",$str);//去除CSS樣式、JS指令碼、HTML注釋
  $alltext="";//用於儲存TXT文本的變數
  $start=1;//用於檢測<左、>右標籤的控制開關
  for($i=0;$i<strlen($str);$i++){//遍曆經過處理後的字串中的每一個字元
    if(($start==0)&&($str[$i]==">")){//如果檢測到>右標籤,則使用$start=1;開啟截取功能
      $start=1;
    }else if($start==1){//截取功能
      if($str[$i]=="<"){//如果字元是<左標籤,則使用<font color='red'>|</font>替換
        $start=0;
        $alltext.="<font color='red'>|</font>";
      }else if(ord($str[$i])>31){//如果字元是ASCII大於31的有效字元,則將字元添加到$alltext變數中
        $alltext.=$str[$i];
      }
    }
}
//下方是去除空格和一些特殊字元的操作
$alltext = str_replace(" "," ",$alltext);
$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
$alltext = preg_replace("/[ ]+/s"," ",$alltext);
return $alltext;
}

使用上面這個方法也可以實現將簡答的HTML代碼轉換為TXT文本。

例3

 代碼如下 複製代碼

function html2text($str,$encode = 'GB2312')
{

  $str = preg_replace("/<style .*?</style>/is", "", $str);
  $str = preg_replace("/<script .*?</script>/is", "", $str);
  $str = preg_replace("/<br s*/?/>/i", "n", $str);
  $str = preg_replace("/</?p>/i", "nn", $str);
  $str = preg_replace("/</?td>/i", "n", $str);
  $str = preg_replace("/</?div>/i", "n", $str);
  $str = preg_replace("/</?blockquote>/i", "n", $str);
  $str = preg_replace("/</?li>/i", "n", $str);

  $str = preg_replace("/&nbsp;/i", " ", $str);
  $str = preg_replace("/&nbsp/i", " ", $str);

  $str = preg_replace("/&amp;/i", "&", $str);
  $str = preg_replace("/&amp/i", "&", $str);

  $str = preg_replace("/&lt;/i", "<", $str);
  $str = preg_replace("/&lt/i", "<", $str);

  $str = preg_replace("/&ldquo;/i", '"', $str);
  $str = preg_replace("/&ldquo/i", '"', $str);

     $str = preg_replace("/&lsquo;/i", "'", $str);
     $str = preg_replace("/&lsquo/i", "'", $str);

     $str = preg_replace("/&rsquo;/i", "'", $str);
     $str = preg_replace("/&rsquo/i", "'", $str);

  $str = preg_replace("/&gt;/i", ">", $str);
  $str = preg_replace("/&gt/i", ">", $str);

  $str = preg_replace("/&rdquo;/i", '"', $str);
  $str = preg_replace("/&rdquo/i", '"', $str);

  $str = strip_tags($str);
  $str = html_entity_decode($str, ENT_QUOTES, $encode);
  $str = preg_replace("/&#.*?;/i", "", $str);
    
  return $str;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.