DDR爱好者之家 Design By 杰米

本文实例讲述了php将html转成wml的WAP标记语言的方法。分享给大家供大家参考。具体实现方法如下:

<"/<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 .*", "", $content);
  $content = preg_replace("/<script .*", "", $content);
  // 首先将各种可以引起换行的标签(如<br />、<p> 之类)替换成换行符"\\n"
  $content = preg_replace("/<br \\s*\\/", "\\n", $content);
  $content = preg_replace("/<\\/", "\\n", $content);
  $content = preg_replace("/<\\/", "\\n", $content);
  $content = preg_replace("/<\\/", "\\n", $content);
  $content = preg_replace("/<\\/", "\\n", $content);
  $content = preg_replace("/<\\/", "\\n", $content);
  // 将"&nbsp;"替换为空格
  $content = preg_replace("/\\&nbsp\\;/i", " ", $content);
  $content = preg_replace("/\\&nbsp/i", " ", $content);
  // 过滤掉剩下的 HTML 标签
  $content = strip_tags($content);
  // 将 HTML 中的实体(entity)转化为它所对应的字符
  $content = html_entity_decode($content, ENT_QUOTES, "GB2312");
  // 过滤掉不能转化的实体(entity)
  $content = preg_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])) "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] = "";
  }
  //合并各行,转化为 WML,并过滤掉空行
  $content = str_replace("<p><br /></p>\\n", "", "<p>".implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");
  return $content;
}
?>

希望本文所述对大家的php程序设计有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米