A WAP markup Language instance that PHP converts HTML into WML _php tips

Source: Internet
Author: User

The example in this article describes the way PHP converts HTML to a WAP markup language for WML. Share to everyone for your reference. The implementation method is as follows:

<?php//---------------------------------------//Html Tag WAP language//----------------------------------------
  function Html2wml ($content) {//Reserved picture 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);
  //filter out style sheet and script $content = Preg_replace ("/<style. *?<\\/style>/is", "", $content);
  $content = Preg_replace ("/<script. *?<\\/script>/is", "", $content); A variety of labels (such as <br/>, <p>, etc.) that can cause a newline are replaced first with the newline character "\\n" $content = Preg_replace ("/<br \\s*\\/?\\/>/i", "\\n", $c
  Ontent);
  $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);
  Replace " " with a space $content = Preg_replace ("/\\ \\;/i", "", $content);
  $content = Preg_replace ("/\\ /i", "", $content);
  Filter out the remaining HTML tags $content = strip_tags ($content);
  Converts an entity in HTML (entity) to its corresponding character $content = Html_entity_decode ($content, Ent_quotes, "GB2312");
  Filter out entities that cannot be transformed (entity) $content = preg_replace ('/\\&\\#.*?\\;/i ', ', ', $content);
  This translates HTML Web page content into plain text with newline, and the following is the conversion of these plain text to 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 you remove the full-width spaces as blank lines, the blank lines are set, otherwise the full-width spaces are not filtered.
  if (Str_replace (', ', $content [$i]) = = ') $content [$i] = '; $content = Str_replace ("<p><br/></p>\\n", "" "," <p> ". Implode (" <br/></p>\\n< P> ", $content)."
  &LT;BR/></p>\\n "); Restore picture if (isset ($imgarr [0]) && COunt ($imgarr [0]) >0) {foreach ($imgarr [0] as $k => $v) {$attstr = (preg_match (' #/$# ', $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 + +) {//filter the trailing space $content [$i] = Trim ($content [$i]);
  If you remove the full-width spaces as blank lines, the blank lines are set, otherwise the full-width spaces are not filtered.
  if (Str_replace ("", "", $content [$i]) = = "") $content [$i] = ""; //merge rows, convert to WML, and filter out empty rows $content = Str_replace ("<p><br/></p>\\n", "", "<p>". Implode ("&LT;BR/ ></p>\\n<p> ", $content)."
  &LT;BR/></p>\\n ");
return $content;

 }?>

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.