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)."
<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 ("<BR/ ></p>\\n<p> ", $content)."
<BR/></p>\\n ");
return $content;
}?>
I hope this article will help you with your PHP programming.