溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用php怎么將html轉(zhuǎn)換成wml

發(fā)布時間:2021-02-26 14:54:01 來源:億速云 閱讀:128 作者:戴恩恩 欄目:開發(fā)技術

這篇文章主要為大家詳細介紹了使用php怎么將html轉(zhuǎn)換成wml,文中示例代碼介紹的非常詳細,具有一定的參考價值,發(fā)現(xiàn)的小伙伴們可以參考一下:

html有什么特點

1、簡易性:超級文本標記語言版本升級采用超集方式,從而更加靈活方便,適合初學前端開發(fā)者使用。 2、可擴展性:超級文本標記語言的廣泛應用帶來了加強功能,增加標識符等要求,超級文本標記語言采取子類元素的方式,為系統(tǒng)擴展帶來保證。  3、平臺無關性:超級文本標記語言能夠在廣泛的平臺上使用,這也是萬維網(wǎng)盛行的一個原因。 4、通用性:HTML是網(wǎng)絡的通用語言,它允許網(wǎng)頁制作人建立文本與圖片相結(jié)合的復雜頁面,這些頁面可以被網(wǎng)上任何其他人瀏覽到,無論使用的是什么類型的電腦或瀏覽器。

具體實現(xiàn)方法如下:

<?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);
  // 將"&nbsp;"替換為空格
  $content = preg_replace("/\\&nbsp\\;/i", " ", $content);
  $content = preg_replace("/\\&nbsp/i", " ", $content);
  // 過濾掉剩下的 HTML 標簽
  $content = strip_tags($content);
  // 將 HTML 中的實體(entity)轉(zhuǎn)化為它所對應的字符
  $content = html_entity_decode($content, ENT_QUOTES, "GB2312");
  // 過濾掉不能轉(zhuǎn)化的實體(entity)
  $content = preg_replace('/\\&\\#.*?\\;/i', '', $content);
  // 上面是將 HTML 網(wǎng)頁內(nèi)容轉(zhuǎn)化為帶換行的純文本,下面是將這些純文本轉(zhuǎn)化為 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("/&amp;[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] = "";
  }
  //合并各行,轉(zhuǎn)化為 WML,并過濾掉空行
  $content = str_replace("<p><br /></p>\\n", "", "<p>".implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");
  return $content;
}
?>

以上就是億速云小編為大家收集整理的使用php怎么將html轉(zhuǎn)換成wml,如何覺得億速云網(wǎng)站的內(nèi)容還不錯,歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI