Php converts html into a wml WAP Markup Language instance, wmlwap

Source: Internet
Author: User

Php converts html into a wml WAP Markup Language instance, wmlwap

This example describes how php converts html into a wml WAP markup language. Share it with you for your reference. The specific implementation method is as follows:

<? Php // tags // Html tag WAP language // ---------------------------------------------- function html2wml ($ content) {// retain the image 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 the style sheet and script $ content = preg_replace ("/<style. *? <\\/ Style>/is "," ", $ content); $ content = preg_replace ("/<script .*? <\\/Script>/is "," ", $ content); // first, you can set various labels that can cause line breaks (such as <br/> and <p>) replace with the line break "\ 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); // Replace" with a space $ content = preg_replace ("// \\\\;/I ", "", $ content); $ content = preg_replace ("// \/I", "", $ content ); // filter out the remaining HTML Tag $ content = strip_tags ($ content); // convert the entity in HTML) convert it to the corresponding character $ content = html_entity_decode ($ content, ENT_QUOTES, "GB2312"); // filter out entities that cannot be converted (entity) $ content = preg_replace ('/\\&\\#. *? \\;/I ', '', $ content); // the above section converts the HTML webpage content to plain text with line breaks. The following section converts the 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 the blank space is removed, the blank line is set, otherwise, the full-width space is 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 the image 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 spaces at the beginning and end $ content [$ I] = trim ($ content [$ I]); // If empty rows are removed, empty rows are set. Otherwise, spaces are not filtered out. 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 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.