php將html轉(zhuǎn)成wml的WAP標(biāo)記語言實(shí)例
本文實(shí)例講述了php將html轉(zhuǎn)成wml的WAP標(biāo)記語言的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<?php
//---------------------------------------
// Html 標(biāo)記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);
// 首先將各種可以引起換行的標(biāo)簽(如<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 標(biāo)簽
$content = strip_tags($content);
// 將 HTML 中的實(shí)體(entity)轉(zhuǎn)化為它所對(duì)應(yīng)的字符
$content = html_entity_decode($content, ENT_QUOTES, "GB2312");
// 過濾掉不能轉(zhuǎn)化的實(shí)體(entity)
$content = preg_replace('/\\&\\#.*?\\;/i', '', $content);
// 上面是將 HTML 網(wǎng)頁內(nèi)容轉(zhuǎn)化為帶換行的純文本,下面是將這些純文本轉(zhuǎn)化為 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]);
// 如果去掉全角空格為空行,則設(shè)為空行,否則不對(duì)全角空格過濾。
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]);
// 如果去掉全角空格為空行,則設(shè)為空行,否則不對(duì)全角空格過濾。
if (str_replace(" ", "", $content[$i]) == "") $content[$i] = "";
}
//合并各行,轉(zhuǎn)化為 WML,并過濾掉空行
$content = str_replace("<p><br /></p>\\n", "", "<p>".implode("<br /></p>\\n<p>", $content)."<br /></p>\\n");
return $content;
}
?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP文件去掉PHP注釋空格的函數(shù)分析(PHP代碼壓縮)
我自己嘗試過正則,但是發(fā)現(xiàn)在過濾單行注釋等方面不盡如意,很容易出錯(cuò)。無意中看到了某sns里面的strip_whitespace函數(shù),特進(jìn)行分享,希望能對(duì)需要的朋友有所幫助2013-07-07
解析php中用PHPMailer來發(fā)送郵件的示例(126.com的例子)
本篇文章是對(duì)php中用PHPMailer來發(fā)送郵件的示例(126.com的例子)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP使用OB緩存實(shí)現(xiàn)靜態(tài)化功能示例
這篇文章主要介紹了PHP使用OB緩存實(shí)現(xiàn)靜態(tài)化功能,結(jié)合具體實(shí)例形式分析了php基于OB緩存讀取數(shù)據(jù)庫并生成靜態(tài)文件相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
PHP使用微信開發(fā)模式實(shí)現(xiàn)搜索已發(fā)送圖文及匹配關(guān)鍵字回復(fù)的方法
這篇文章主要介紹了PHP使用微信開發(fā)模式實(shí)現(xiàn)搜索已發(fā)送圖文及匹配關(guān)鍵字回復(fù)的方法,涉及php針對(duì)微信json格式數(shù)據(jù)的解析與正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Apache實(shí)現(xiàn)Web Server負(fù)載均衡詳解(不考慮Session版)
本篇文章是對(duì)使用Apache實(shí)現(xiàn)Web Server負(fù)載均衡的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下(不考慮Session版)2013-07-07

