当前位置:Gxlcms > PHP教程 > php将html转成wml的WAP标记语言实例

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

时间:2021-07-01 10:21:17 帮助过:21人阅读

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

  1. <?php
  2. //---------------------------------------
  3. // Html 标记WAP语言
  4. //----------------------------------------
  5. function html2wml($content)
  6. {
  7. //保留图片
  8. preg_match_all("/<img([^>]*)>/isU", $content, $imgarr);
  9. if(isset($imgarr[0]) && count($imgarr[0])>0 )
  10. {
  11. foreach($imgarr[0] as $k=>$v) $content = str_replace($v, "WAP-IMG::{$k}", $content);
  12. }
  13. // 过滤掉样式表和脚本
  14. $content = preg_replace("/<style .*?<\\/style>/is", "", $content);
  15. $content = preg_replace("/<script .*?<\\/script>/is", "", $content);
  16. // 首先将各种可以引起换行的标签(如<br />、<p> 之类)替换成换行符"\\n"
  17. $content = preg_replace("/<br \\s*\\/?\\/>/i", "\\n", $content);
  18. $content = preg_replace("/<\\/?p>/i", "\\n", $content);
  19. $content = preg_replace("/<\\/?td>/i", "\\n", $content);
  20. $content = preg_replace("/<\\/?div>/i", "\\n", $content);
  21. $content = preg_replace("/<\\/?blockquote>/i", "\\n", $content);
  22. $content = preg_replace("/<\\/?li>/i", "\\n", $content);
  23. // 将" "替换为空格
  24. $content = preg_replace("/\\ \\;/i", " ", $content);
  25. $content = preg_replace("/\\ /i", " ", $content);
  26. // 过滤掉剩下的 HTML 标签
  27. $content = strip_tags($content);
  28. // 将 HTML 中的实体(entity)转化为它所对应的字符
  29. $content = html_entity_decode($content, ENT_QUOTES, "GB2312");
  30. // 过滤掉不能转化的实体(entity)
  31. $content = preg_replace('/\\&\\#.*?\\;/i', '', $content);
  32. // 上面是将 HTML 网页内容转化为带换行的纯文本,下面是将这些纯文本转化为 WML。
  33. $content = str_replace('$', '$$', $content);
  34. $content = str_replace("\\r\\n", "\\n", htmlspecialchars($content));
  35. $content = explode("\\n", $content);
  36. for ($i = 0; $i < count($content); $i++)
  37. {
  38. $content[$i] = trim($content[$i]);
  39. // 如果去掉全角空格为空行,则设为空行,否则不对全角空格过滤。
  40. if (str_replace(' ', '', $content[$i]) == '') $content[$i] = '';
  41. }
  42. $content = str_replace("<p><br /></p>\\n", "", '<p>'.implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");
  43. //还原图片
  44. if(isset($imgarr[0]) && count($imgarr[0])>0 )
  45. {
  46. foreach($imgarr[0] as $k=>$v)
  47. {
  48. $attstr = (preg_match('#/$#', $imgarr[1][$k])) ? '<img '.$imgarr[1][$k].'>' : '<img '.$imgarr[1][$k].' />';
  49. $content = str_replace("WAP-IMG::{$k}", $attstr, $content);
  50. }
  51. }
  52. $content = preg_replace("/&[a-z]{3,10};/isU", ' ', $content);
  53. return $content;
  54. }
  55. function text2wml($content)
  56. {
  57. $content = str_replace('$', '$$', $content);
  58. $content = str_replace("\\r\\n", "\\n", htmlspecialchars($content));
  59. $content = explode("\\n", $content);
  60. for ($i = 0; $i < count($content); $i++)
  61. {
  62. // 过滤首尾空格
  63. $content[$i] = trim($content[$i]);
  64. // 如果去掉全角空格为空行,则设为空行,否则不对全角空格过滤。
  65. if (str_replace(" ", "", $content[$i]) == "") $content[$i] = "";
  66. }
  67. //合并各行,转化为 WML,并过滤掉空行
  68. $content = str_replace("<p><br /></p>\\n", "", "<p>".implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");
  69. return $content;
  70. }
  71. ?>

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

人气教程排行