php將html轉成wml的WAP標記語言執行個體_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php將html轉成wml的WAP標記語言的方法。分享給大家供大家參考。具體實現方法如下:

<?php//---------------------------------------// Html 標記WAP語言//----------------------------------------function html2wml($content){  //保留圖片  preg_match_all("/<img([^>]*)>/isU", $content, $imgarr);  if(isset($imgarr[0]) && count($imgarr[0])>0 )  {   foreach($imgarr[0] as $k=>$v) $content = str_replace($v, "WAP-IMG::{$k}", $content);  }  // 過濾掉樣式表和指令碼  $content = preg_replace("/<style .*?<\\/style>/is", "", $content);  $content = preg_replace("/<script .*?<\\/script>/is", "", $content);  // 首先將各種可以引起換行的標籤(如<br />、<p> 之類)替換成分行符號"\\n"  $content = preg_replace("/<br \\s*\\/?\\/>/i", "\\n", $content);  $content = preg_replace("/<\\/?p>/i", "\\n", $content);  $content = preg_replace("/<\\/?td>/i", "\\n", $content);  $content = preg_replace("/<\\/?div>/i", "\\n", $content);  $content = preg_replace("/<\\/?blockquote>/i", "\\n", $content);  $content = preg_replace("/<\\/?li>/i", "\\n", $content);  // 將" "替換為空白格  $content = preg_replace("/\\ \\;/i", " ", $content);  $content = preg_replace("/\\ /i", " ", $content);  // 過濾掉剩下的 HTML 標籤  $content = strip_tags($content);  // 將 HTML 中的實體(entity)轉化為它所對應的字元  $content = html_entity_decode($content, ENT_QUOTES, "GB2312");  // 過濾掉不能轉化的實體(entity)  $content = preg_replace('/\\&\\#.*?\\;/i', '', $content);  // 上面是將 HTML 網頁內容轉化為帶換行的純文字,下面是將這些純文字轉化為 WML。  $content = str_replace('$', '$$', $content);  $content = str_replace("\\r\\n", "\\n", htmlspecialchars($content));  $content = explode("\\n", $content);  for ($i = 0; $i < count($content); $i++)  {  $content[$i] = trim($content[$i]);  // 如果去掉全形空格為空白行,則設為空白行,否則不對全形空格過濾。  if (str_replace(' ', '', $content[$i]) == '') $content[$i] = '';  }  $content = str_replace("<p><br /></p>\\n", "", '<p>'.implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");  //還原圖片  if(isset($imgarr[0]) && count($imgarr[0])>0 )  {    foreach($imgarr[0] as $k=>$v)    {     $attstr = (preg_match('#/$#', $imgarr[1][$k])) ? '<img '.$imgarr[1][$k].'>' : '<img '.$imgarr[1][$k].' />';     $content = str_replace("WAP-IMG::{$k}", $attstr, $content);    }  }  $content = preg_replace("/&[a-z]{3,10};/isU", ' ', $content);  return $content;}function text2wml($content){  $content = str_replace('$', '$$', $content);  $content = str_replace("\\r\\n", "\\n", htmlspecialchars($content));  $content = explode("\\n", $content);  for ($i = 0; $i < count($content); $i++)  {  // 過濾首尾空格  $content[$i] = trim($content[$i]);  // 如果去掉全形空格為空白行,則設為空白行,否則不對全形空格過濾。  if (str_replace(" ", "", $content[$i]) == "") $content[$i] = "";  }  //合并各行,轉化為 WML,並過濾掉空行  $content = str_replace("<p><br /></p>\\n", "", "<p>".implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");  return $content;}?>

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.