首页 > php教程 > php手册 > 正文

php将html转成wml的WAP标记语言实例,wmlwap

php中文网
发布: 2016-06-13 08:58:25
原创
899人浏览过

php将html转成wml的wap标记语言实例,wmlwap

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

<&#63;php
//---------------------------------------
// Html 标记WAP语言
//----------------------------------------
function html2wml($content)
{
  //保留图片
  preg_match_all("/@@##@@]*)>/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 .*&#63;<\/style>/is", "", $content);
  $content = preg_replace("/<script .*&#63;<\/script>/is", "", $content);
  // 首先将各种可以引起换行的标签(如<br />、<p> 之类)替换成换行符"\n"
  $content = preg_replace("/<br \s*\/&#63;\/>/i", "\n", $content);
  $content = preg_replace("/<\/&#63;p>/i", "\n", $content);
  $content = preg_replace("/<\/&#63;td>/i", "\n", $content);
  $content = preg_replace("/<\/&#63;div>/i", "\n", $content);
  $content = preg_replace("/<\/&#63;blockquote>/i", "\n", $content);
  $content = preg_replace("/<\/&#63;li>/i", "\n", $content);
  // 将" "替换为空格
  $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('/\&\#.*&#63;\;/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><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</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])) &#63; '@@##@@' : '@@##@@';
     $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><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>\n", "", "<p>".implode("<br /></p>\n<p>", $content)."<br /></p>\n");
  return $content;
}
&#63;>

登录后复制

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

php将html转成wml的WAP标记语言实例,wmlwapphp将html转成wml的WAP标记语言实例,wmlwapphp将html转成wml的WAP标记语言实例,wmlwap
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号