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

yipeiwu_com5年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

PHP简单读取xml文件的方法示例

本文实例讲述了PHP简单读取xml文件的方法。分享给大家供大家参考,具体如下: 我将软件版本更新中的版本号等数据信息存放在xml文件中,使用时将版本信息读取出来。 xml文件内容如下:...

PHP登录环节防止sql注入的方法浅析

在防止sql注入这些细节出现问题的一般是那些大意的程序员或者是新手程序员,他们由于没有对用户提交过来的数据进行一些必要的过滤,从而导致了给大家测试的时候一下就攻破了你的数据库,下面我们来...

php Sql Server连接失败问题及解决办法

1、确认数据库服务开启状态 2、php.ini配置中的扩展打开 3、检查数据库相关的版本 (1)Sql2000此时要检查php目录和apache的bin目录下的ntwdblib.dll文...

在WordPress中实现发送http请求的相关函数解析

在 PHP 中发送 Http 请求(GET / POST)有很多的方法,比如 file_get_contents() 函数、fopen() 函数或者 cURL 扩展,但由于服务器的情况不...

PHP弹出对话框技巧详细解读

许多程序员们运用PHP语言进行WEB开发。那么在网站中通常都会遇到对话框弹出等功能需求。下面我们就为大家详细介绍PHP弹出对话框的相关实现方法。 1. PHP弹出对话框 < &...