程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP 將 HTML 代碼 轉換到 UBB 論壇代碼

PHP 將 HTML 代碼 轉換到 UBB 論壇代碼

編輯:關於PHP編程

烈16火84網(LieHuo24.Net)教17程 最近給下載欄目增設一鍵轉帖功能,烈火下載欄目是使用DedeCMS構建的,大家都知道dede沒有一鍵轉帖功能,於是只好自己寫一個,最終實現了。這裡記下其中軟件簡介的HTML轉UBB代碼。

一鍵轉帖演示:

關於PHP將HTML轉UBB代碼的問題,筆者在網上找到幾個,但都不太好用,於是自己進行了修改和整理,如您有其它需求,可在下面留言。

PHP代碼:

Copy to ClipboardLiehuo.Net Codes引用的內容:[www.bkjia.com] <?php
$intro = $liehuo-net; // 這裡是你獲取的變量值。
$intro = str_replace("\n",'',$intro);
$intro = preg_replace("/\<A[^>]+HREF=\"([^\"]+)\"[^>]*\>(.*?)<\/a\>/i","[url=$1]$2[/url]",$intro);
$intro = preg_replace("/\<font(.*?)color=\"#([^ >]+)\"(.*?)\>(.*?)<\/font>/i","<font$1$3>[color=$2]$4[/color]</font>",$intro);
$intro = preg_replace("/\<font(.*?)face=\"([^ >]+)\"(.*?)\>(.*?)<\/font>/i","<font$1$3>[face=$2]$4[/face]</font>",$intro);
$intro = preg_replace("/\<font(.*?)size=\"([^ >]+)\"(.*?)\>(.*?)<\/font>/i","[size=$2]$4[/size]",$intro);
$intro = preg_replace("/\<img[^>]+src=\"([^\"]+)\"[^>]*\>/i","[img]$1[/img]",$intro);
$intro = preg_replace("/\<DIV[^>]+ALIGN=\"([^\"]+)\"[^>]*\>(.*?)<\/DIV\>/i","[align=$1]$2[/align]",$intro);
$intro = preg_replace("/\<P[^>]+ALIGN=\"([^\"]+)\"[^>]*\>(.*?)<\/P\>/i","[align=$1]$2[/align]",$intro);
$intro = preg_replace("/\<([\/]?)u\>/i","[$1u]",$intro);
$intro = preg_replace("/\<([\/]?)em\>/i","[$1I]",$intro);
$intro = preg_replace("/\<([\/]?)strong\>/i","[$1b]",$intro);
$intro = preg_replace("/\<([\/]?)i\>/i","[$1i]",$intro);
echo $intro; // 最後得到UBB代碼
?>

現在我們知道了HTML到UBB的轉換,那麼UBB是否能返回HTML呢?請參照下面的代碼,網上收集的。

代碼一:

Copy to ClipboardLiehuo.Net Codes引用的內容:[www.bkjia.com] <?php
function ubb2html($strCodes) {
$match = array(
"%\[b\](.*?)\[\/b\]%si",
"%\[center\](.*?)\[\/center\]%si",
"%\[url\](.*?)\[\/url\]%si",
"%\[url=(.*?)\](.*?)\[\/url\]%si",
"%\[img\](.*?)\[\/img\]%si",
"%\[fieldset=(.*?)\](.*?)\[\/fieldset\]%si"

);
$replace = array(
"<b>$1</b>",
"<center>$1</center>",
"<a href=\"$1\" target=_blank>$1</a>",
"<a href=\"$1\" target=_blank>$2</a>",
"<a href=\"$1\" target=\"_blank\"><img src=\"$1\" title="烈火提示:點擊查看" border=\"0\" onload=\"javascript:if(this.width>550)this.width=500\" title=\"烈火提示:點擊查看\"></a>",
"<fieldset><legend>$1</legend><blockquote>$2</blockquote></fieldset>"
);
$strCodes = preg_replace($match, $replace, $strCodes);

return $strCodes ;
}
?>

代碼二:

Copy to ClipboardLiehuo.Net Codes引用的內容:[www.bkjia.com] <?php
function ubb2html($sUBB)
{
$sHtml=$sUBB;
$sHtml=preg_replace("/</",'<',$sHtml);
$sHtml=preg_replace("/>/",'>',$sHtml);
$sHtml=preg_replace("/\r?\n/",'<br />',$sHtml);
$sHtml=preg_replace("/\[(\/?)(b|u|i|s|sup|sub)\]/i",'<$1$2>',$sHtml);
$sHtml=preg_replace("/\[color\s*=\s*([^\]]+?)\]/i",'<span style="color:$1;">',$sHtml);
function getSizeName($match)
{
$arrSize=array('8pt','10pt','12pt','14pt','18pt','24pt','36pt');
return '<span style="font-size:'.$arrSize[$match[1]-1].';">';
}
$sHtml=preg_replace_callback("/\[size\s*=\s*(\d+?)\]/i",'getSizeName',$sHtml);
$sHtml=preg_replace("/\[font\s*=\s*([^\]]+?)\]/i",'<span style="font-family:$1;">',$sHtml);
$sHtml=preg_replace("/\[back\s*=\s*([^\]]+?)\]/i",'<span style="background-color:$1;">',$sHtml);
$sHtml=preg_replace("/\[\/(color|size|font|back)\]/i",'</span>',$sHtml);

for($i=0;$i<3;$i++)$sHtml=preg_replace("/\[align\s*=\s*([^\]]+?)\](((?!\[align(?:\s+[^\]]+)?\])[\s\S])*?)\[\/align\]/",'<p align="$1">$2</p>',$sHtml);
$sHtml=preg_replace("/\[img\]\s*([\s\S]+?)\s*\[\/img\]/i",'<img src="$1" />',$sHtml);
function getImg($match)
{
$p1=$match[1];$p2=$match[2];$p3=$match[3];$src=$match[4]; title="烈火提示:點擊查看"
$a=$p3?$p3:($p2?$p1:'');
return '<img src="'.$src.'"'.($p2?' width="'.$p1.'" height="'.$p2.'"':'').($a?' align="'.$a.'"':'').' />';
}
$sHtml=preg_replace_callback("/\[img\s*=(?:\s*(\d+)\s*,\s*(\d+)\s*)?(?:,?\s*(\w+)\s*)?\]\s*([\s\S]+?)\s*\[\/img\]/i",'getImg',$sHtml);
$sHtml=preg_replace("/\[url\]\s*([\s\S]+?)\s*\[\/url\]/i",'<a href="$1">$1</a>',$sHtml);
$sHtml=preg_replace("/\[url\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/url\]/i",'<a href="$1">$2</a>',$sHtml);
$sHtml=preg_replace("/\[email\]\s*([\s\S]+?)\s*\[\/email\]/i",'<a href="mailto:$1">$1</a>',$sHtml);
$sHtml=preg_replace("/\[email\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/email\]/i",'<a href="mailto:$1">$2</a>',$sHtml);
$sHtml=preg_replace("/\[quote\]([\s\S]*?)\[\/quote\]/i",'<blockquote>$1</blockquote>',$sHtml);
$sHtml=preg_replace("/\[code\]([\s\S]*?)\[\/code\]/i",'<code>$1</code>',$sHtml);
function getFlash($match)
{
$w=$match[1];$h=$match[2];$url=$match[3];
if(!$w)$w=550;if(!$h)$h=400;
return '<embed type="application/x-shockwave-flash" src="'.$url.'" wmode="opaque" quality="high" bgcolor="#ffffff" menu="false" play="true" loop="true" width="'.$w.'" height="'.$h.'"/>';
}
$sHtml=preg_replace_callback("/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]([\s\S]+?)\[\/flash\]/i",'getFlash',$sHtml);
function getMedia($match)
{
$w=$match[1];$h=$match[2];$play=$match[3];$url=$match[4];
if(!$w)$w=550;if(!$h)$h=400;
return '<embed type="application/x-mplayer2" src="'.$url.'" enablecontextmenu="false" autostart="'.($play=='1'?'true':'false').'" width="'.$w.'" height="'.$h.'"/>';
}
$sHtml=preg_replace_callback("/\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?)?\]([\s\S]+?)\[\/media\]/i",'getMedia',$sHtml);
function getTable($match)
{
$w=$match[1];$b=$match[2];
$str='<table';
if($w)$str.=' width="'.$w.'"';
if($b)$str.=' bgcolor="'.$b.'"';
return $str.'>';
}
$sHtml=preg_replace_callback("/\[table(?:\s*=\s*(\d{1,4}%?)\s*(?:,\s*([^\]]+)\s*)?)?]/i",'getTable',$sHtml);
function getTR($match){return '<tr'.($match[1]?' bgcolor="'.$match[1].'"':'').'>';}
$sHtml=preg_replace_callback("/\[tr(?:\s*=(\s*[^\]]+))?\]/i",'getTR',$sHtml);
function getTD($match){
$col=$match[1];$row=$match[2];$w=$match[3];
return '<td'.($col>1?' colspan="'.$col.'"':'').($row>1?' rowspan="'.$row.'"':'').($w?' width="'.$w.'"':'').'>';
}
$sHtml=preg_replace_callback("/\[td(?:\s*=\s*(\d{1,2})\s*,\s*(\d{1,2})\s*(?:,\s*(\d{1,4}%?))?)?\]/i",'getTD',$sHtml);
$sHtml=preg_replace("/\[\/(table|tr|td)\]/i",'</$1>',$sHtml);
$sHtml=preg_replace("/\[\*\]([^\[]+)/i",'<li>$1</li>',$sHtml);
function getUL($match)
{
$str='<ul';
if($match[1])$str.=' type="'.$match[1].'"';
return $str.'>';
}
$sHtml=preg_replace_callback("/\[list(?:\s*=\s*([^\]]+)\s*)?\]/i",'getUL',$sHtml);
$sHtml=preg_replace("/\[\/list\]/i",'</ul>',$sHtml);
return $sHtml;
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved