程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 一個UBB的PHP類

一個UBB的PHP類

編輯:PHP綜合
/*
Text Encode Class
Write by q3boy 2003.3.10
usage:
$eq = new EncodeQ3boy($str);初始化類
//以下為ubbEncode參數
$eq->url = true; //啟用url自動解析 默認false
$eq->html = true; //啟用HTML編碼(處理<,>,全角/半角空格,制表符,換行符)默認true
$eq->image = true; //啟用圖象標簽解析 默認true
$eq->font = true; //啟用字體標簽解析 默認true
$eq->element = true; //啟用外部元素解析 默認true
$eq->flash = true; //啟用Flash解析 默認true
$eq->php = true; //啟用語法高亮顯示 默認true
//ubbEncode參數結束
echo($eq->getImageOpener());//輸出圖片自動縮放所需js函數
echo $eq->htmlEncode(); //輸出ubb編碼後字符串
echo"<hr>";
echo $eq->ubbEncode(); //輸出ubb編碼後字符串
echo"<hr>";
echo $eq->removeHtml(); //輸出移除html標簽的字符串
echo"<hr>";
echo $eq->ubbEncode(); //輸出移除ubb標簽的字符串
支持ubb標簽列表:
圖片類:
[img]http://www.aaa.com/aaa.gif[/img] 插入圖片
[limg]http://www.aaa.com/aaa.gif[/limg] 圖片左繞排
[rimg]http://www.aaa.com/aaa.gif[/rimg] 圖片右繞排
[cimg]http://www.aaa.com/aaa.gif[/cimg] 圖片居中繞排
文本控制類:
[br] 換行符
[b]粗體字[b]
[i]斜體字[i]
[u]下劃線[u]
[s]刪除線[s]
[sub]文字下標[sub]
[sup]文字上標[sup]
[left]文字左對齊[left]
[right]文字右對齊[right]
[center]文字居中[center]
[align=(left|center|right)]文字對齊方式[align]
[size=([1-6])]文字大小[size]
[font=(字體)[font]
[color=(文字顏色)][color]
[list]無序列表[list]
[list=s]有序列表[list]
[list=(A|1|I)]有序列表(列表方式為(abc,123,I II III))[list]
[list=(num)]有序列表(自num開始計數)[list]
[li]列表單元項[li]
外部元素類:
[url]鏈接[/url]
[url=(鏈接)]鏈接文字[/url]
[email]郵件地址[/email]
[email=(郵件地址)]說明文字[/email]郵件地址
[quote]引用塊[/quote]
[iframe]內插幀地址[/iframe]
[iframe=(內插幀高度)]內插幀地址[/iframe]
[swf]flash動畫地址[/swf]
[swf=寬度,高度]flash動畫地址[/swf]
代碼塊:
[code][/code]
[php][/php]
[code 代碼塊名稱][/code]
[php 代碼塊名稱][/php]
如需使用php語法高亮請務必在代碼塊兩端加上"<??>"標簽
*/
class EncodeQ3boy {
var $str = "";
var $iconpath = "/image/icon";//圖標文件路徑
var $imagepath = "/upfiles/article";//圖片文件默認路徑
var $tagfoot = ' border="1" onload="ImageLoad(this);" onClick="ImageOpen(this)" style="cursor: hand" ';//圖片文件附加屬性
var $url = false; //url自動解析
var $html = true; //HTML編碼
var $image = true; //解析圖象標簽
var $font = true; //字體標簽
var $element = true; //外部元素
var $flash = true; //Flash
var $php = true; //語法高亮顯示
function EncodeQ3boy($str='',$imgph='') {
if($str) {
$str = strtr($str,array("\n\r"=>"\n","\r\n"=>"\n","\r"=>"\n"," "=>" "));
$this->str = $str;
}
if($imgph) $this->imagepath = $imgph;
}
function getImageOpener() {
Return "<script language=\"javascript\" type=\"text/javascript\">\r\nfunction ImageLoad(img) {\r\nif(img.width>480) img.width=480;\r\n}\r\nfunction ImageOpen(img) {\r\nwindow.open(img.src,'','menubar=no,scrollbars=yes,width='+(screen.width-8)+',height='+(screen.height-74)+',left=0,top=0');\r\n}\r\n</script>";
}
function removeHtml($str='') {
if(!$str) $str = $this->str;
return strip_tags($str);
}
function removeUbb($str='') {
if(!$str) $str = $this->str;
return preg_replace("/\[\/?\w+(\s+[^\]\s]+)*\s*\]/is","",$str);
}
function htmlEncode($str='') {
if(!$str) $str = $this->str;
$ary = array(
'<'=>'&lt;',
'>'=>'&gt;',
" "=>"&nbsp;&nbsp;",
" "=>"&nbsp;&nbsp;"
);
$str = preg_replace("/\n{2,}/s","\n\n",strtr($str,$ary));
Return str_replace("\n","\n<br />",$str);
}
function ubbEncode($str='') {
if(!$str) $str = $this->str;
$rpl_ary = array();
$rpl_ary = array();
if($this->html) $str = $this->htmlEncode($str,true);
$tagfoot = $this->tagfoot;
$icon = $this->iconpath;
$image = $this->imagepath;
if($this->php) {
preg_match_all('/(\n\<br \/\>)*\[(php|code)\s*(.*?)\]\s*(.+?)\s*\[\/(php|code)\](\n\<br \/\>)*/is',$str,$ary);
$str = preg_split('/(\n\<br \/\>)*\[(php|code)\s*(.*?)\]\s*(.+?)\s*\[\/(php|code)\](\n\<br \/\>)*/is',$str);
}
if($this->url){
$reg_ary = array_merge($reg_ary,array(
'/(?<!\]|\=)\s*(\b\w+@(?:\w+\.)+\w{2,3})\b\s*(?<!\[)/i',
'/(?<!\]|\=)\s*(\b(http|https|ftp):\/\/(\w+\.)+\w+(\/[\w|&|%|\?|=|\+|\.|-]+)*)\b\s*(?<!\[)/i',
));
$rpl_ary = array_merge($rpl_ary,array(
'[email]\\1[/email]',
'[url]\\1[/url]',
));
}
if($this->image) {
$reg_ary = array_merge($reg_ary,array(
'/\[img\]\s*http(s?):\/\/(\S+)\s*\[\/img\]/i',
'/\[limg\]\s*http(s?):\/\/(\S+)\s*\[\/limg\]/i',
'/\[rimg\]\s*http(s?):\/\/(\S+)\s*\[\/rimg\]/i',
'/\[cimg\]\s*http(s?):\/\/(\S+)\s*\[\/cimg\]/i',
'/\[img\]\s*([^\/:]+)\s*\[\/img\]/i',
'/\[limg\]\s*([^\/:]+)\s*\[\/limg\]/i',
'/\[rimg\]\s*([^\/:]+)\s*\[\/rimg\]/i',
'/\[cimg\]\s*([^\/:]+)\s*\[\/cimg\]/i',
'/\[img\]\s*(\S+)\s*\[\/img\]/i',
'/\[limg\]\s*(\S+)\s*\[\/limg\]/i',
'/\[rimg\]\s*(\S+)\s*\[\/rimg\]/i',
'/\[cimg\]\s*(\S+)\s*\[\/cimg\]/i',
));
$rpl_ary = array_merge($rpl_ary,array(
'<img src="http\1://\2"'.$tagfoot.'>',
'<img src="http\1://\2"'.$tagfoot.' align="left">',
'<img src="http\1://\2"'.$tagfoot.' align="right">',
'<div align="center"><img src="http\1://\2"'.$tagfoot.'></div>',
'<img src="'.$image.'/\1"'.$tagfoot.'>',
'<img src="'.$image.'/\1"'.$tagfoot.' align="left">',
'<img src="'.$image.'/\1"'.$tagfoot.' align="right">',
'<div align="center"><img src="'.$image.'/\1"'.$tagfoot.'></div>',
'<img src="\1"'.$tagfoot.'>',
'<img src="\1"'.$tagfoot.' align="left">',
'<img src="\1"'.$tagfoot.' align="right">',
'<div align="center"><img src="\1"'.$tagfoot.'></div>',
));
}
if($this->font) {
$reg_ary = array_merge($reg_ary,array(
'/\[br\]/i',
'/\[b\]\s*(.+?)\s*\[\/b\]/is',
'/\[i\]\s*(.+?)\s*\[\/i\]/is',
'/\[u\]\s*(.+?)\s*\[\/u\]/is',
'/\[s\]\s*(.+?)\s*\[\/s\]/is',
'/\[sub\]\s*(.+?)\s*\[\/sub\]/is',
'/\[sup\]\s*(.+?)\s*\[\/sup\]/is',
'/\[left\]\s*(.+?)\s*\[\/left\]/is',
'/\[right\]\s*(.+?)\s*\[\/right\]/is',
'/\[center\]\s*(.+?)\s*\[\/center\]/is',
'/\[align=\s*(left|center|right)\]\s*(.+?)\s*\[\/align\]/is',
'/\[size=\s*([\.|\d])\s*\]\s*(.*?)\s*\[\/size\]/is',
'/\[font=\s*(.*?)\s*\]\s*(.*?)\s*\[\/font\]/is',
'/\[color=\s*(.*?)\s*\]\s*(.*?)\s*\[\/color\]/is',
'/\[list\]\s*(<br \/>)?\s*(.+?)\s*\[\/list\]/is',
'/\[list=s\]\s*(<br \/>)?\s*(.+?)\s*\[\/list\]/is',
'/\[list=(A|1|I)\]\\s*(<br \/>)?\s*(.+?)\s*\[\/list\]/is',
'/\[list=(\S+?)\]\s*(<br \/>)?\s*(.+?)\s*\[\/list\]/is',
'/\[li\]\s*(.+?)\s*\[\/li\]/is',
));
$rpl_ary = array_merge($rpl_ary,array(
'<br />',
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<s>\\1</s>',
'<sub>\\1</sub>',
'<sup>\\1</sup>',
'<div align="left">\\1</div>',
'<div align="right">\\1</div>',
'<div align="center">\\1</div>',
'<div align="\\1">\\2</div>',
'<font size=\\1pt;">\\2</font>',
'<font face="\\1">\\2</font>',
'<font color="\\1">\\2</font>',
'<ul>\\2</ul>',
'<ol>\\2</ol>',
'<ol type="\\1">\\3</ol>',
'<ol start="\\1">\\3</ol>',
'<li>\\1</li>',
));
}
if($this->element){
$reg_ary = array_merge($reg_ary,array(
'/\[url=\s*(.+?)\s*\]\s*(.+?)\s*\[\/url\]/i',
'/\[url]\s*(.+?)\s*\[\/url\]/i',
'/\[email=\s*(.+?)\s*\]\s*(.+?)\s*\[\/email\]/i',
'/\[email]\s*(.+?)\s*\[\/email\]/i',
'/\[quote\]\s*(<br \/>)?\s*(.+?)\s*\[\/quote\]/is',
'/\[iframe\]\s*(.+?)\s*\[\/iframe\]/is',
'/\[iframe=\s*(\d+?)\s*\]\s*(.+?)\s*\[\/iframe\]/is',
));
$rpl_ary = array_merge($rpl_ary,array(
'<a href="\1" target="_blank"><img src="'.$icon.'/url.gif" border="0" alt="超鏈接地址" align="bottom">\2</a> ',
'<a href="\1" target="_blank"><img src="'.$icon.'/url.gif" border="0" alt="超鏈接地址" align="bottom">\1</a> ',
'<a href="mailto:\1"><img src="'.$icon.'/mail.gif" border="0" alt="電子郵件地址" align="bottom">\2</a> ',
'<a href="mailto:\1"><img src="'.$icon.'/mail.gif" border="0" alt="電子郵件地址" align="bottom">\1</a> ',
'<table cellpadding="0" cellspacing="0" border="0" width="90%" align="center" style="border:1px gray solid;"><tr><td><table width="100%" cellpadding="5" cellspacing="1" border="0"><tr><td width="100%">\2</td></tr></table></td></tr></table>',
'<iframe src="\" name="ifr1" frameborder="0" allowtransparency="true" scrolling="yes" width="100%" height="340" marginwidth="0" marginheight="0" hspace="0" vspace="0">\1</iframe><br><a href="\1" target="_blank">如果你的浏覽器不支持嵌入框,請點這裡查看</a>',
'<iframe src="\2" name="ifr1" frameborder="0" allowtransparency="true" scrolling="yes" width="100%" height="\1" marginwidth="0" marginheight="0" hspace="0" vspace="0">\2</iframe><br><a href="\2" target="_blank">如果你的浏覽器不支持嵌入框,請點這裡查看</a>',
));
}
if($this->flash){
$reg_ary = array_merge($reg_ary,array(
'/\[swf\]\s*(.+?)\s*\[\/swf\]/i',
'/\[swf=(\d+)\,(\d+)\]\s*(.+?)\s*\[\/swf\]/i'
));
$rpl_ary = array_merge($rpl_ary,array(
'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"><param name="movie" value="\1" /><param name="quality" value="high" /><embed src="\1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></object>',
'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="\1" height="\2"><param name="movie" value="\3" /><param name="quality" value="high" /><embed src="\3" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="\1" height="\2"></embed></object>'
));
}
if(sizeof($reg_ary)&&sizeof($rpl_ary))$str = preg_replace($reg_ary,$rpl_ary,$str);
if($this->php) {
$tmp = $str[0];
for($i=0; $i<sizeof($ary[4]); $i++) {
ob_start();
highlight_string(trim(strtr($ary[4][$i],array('&lt;'=>'<','&gt;'=>'>',"&nbsp;"=>" ","<br />"=>""))));
$tmp .= '<table border=1 cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#055AA0" width=95%><tr><td><code>'.(trim($ary[3][$i])?trim($ary[3][$i]):'代碼片段:').'</code><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="100%" class="code">'.ob_get_contents().'</td></tr></table></td></tr></table>'.$str[$i+1];
ob_end_clean();
}
$str = $tmp;
unset($tmp);
}
Return $str;
}
}
//上面就是完整的UBB函數
//下面是調用方法,
$u=new EncodeQ3boy();
echo $u->ubbEncode($_POST["ok"]);
<form method=post action="">
<textarea name="ok" rows="6" cols="60"></textarea>
<input type="submit">
</form>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved