一个UBB的PHP类
以下面是源代码调用方法:
$u=new EncodeQ3boy();
echo $u->ubbEncode($_POST["ok"]);
/*
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"
";
echo $eq->ubbEncode(); //输出ubb编码后字符串
echo"
";
echo $eq->removeHtml(); //输出移除html标签的字符串
echo"
";
echo $eq->ubbEncode(); //输出移除ubb标签的字符串
支持ubb标签列表:
图片类:
插入图片
[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]
[font=(字体)[font]
[color=(文字颜色)][color]无序列表
有序列表
有序列表(列表方式为(abc,123,I II III))
有序列表(自num开始计数)
[li]列表单元项[li]
外部元素类:
链接
链接文字
邮件地址
说明文字邮件地址引用块[/quote]
[iframe]内插帧地址[/iframe]
[iframe=(内插帧高度)]内插帧地址[/iframe]
[swf]flash动画地址[/swf]
[swf=宽度,高度]flash动画地址[/swf]
代码块:
[php][/php]
[code 代码块名称][/code]
[php 代码块名称][/php]
如需使用php语法高亮请务必在代码块两端加上"?>"标签
*/
class EncodeQ3boy {
var $str = "";
var $iconpath = "/image/icon";//图标文件路径
var $imagepath = "/upfiles/article";//图片文件默认路径
var $tagfoot = ' border="1" 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("nr"=>"n","rn"=>"n","r"=>"n"," "=>" "));
$this->str = $str;
}
if($imgph) $this->imagepath = $imgph;
}
function getImageOpener() {
Return "";
}
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(
'' '>'=>'>',
" "=>" ",
" "=>" "
);
$str = preg_replace("/n{2,}/s","nn",strtr($str,$ary));
Return str_replace("n","n
",$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
)*[(php|code)s*(.*?)]s*(.+?)s*[/(php|code)](n
)*/is',$str,$ary);
$str = preg_split('/(n
)*[(php|code)s*(.*?)]s*(.+?)s*[/(php|code)](n
)*/is',$str);
}
if($this->url){
$reg_ary = array_merge($reg_ary,array(
'/(? '/(? ));
$rpl_ary = array_merge($rpl_ary,array(
'1',
'1',
));
}
if($this->image) {
$reg_ary = array_merge($reg_ary,array(
'//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',
'//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(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
));
}
if($this->font) {
$reg_ary = array_merge($reg_ary,array(
'/[br]/i',
'/s*(.+?)s*/is',
'/s*(.+?)s*/is',
'/s*(.+?)s*/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',
'/
'/[align=s*(left|center|right)]s*(.+?)s*[/align]/is',
'/[size=s*([.|d])s*]s*(.*?)s*/is',
'/[font=s*(.*?)s*]s*(.*?)s*[/font]/is',
'/s*(.*?)s*/is',
'/s*(
/is',
)?s*(.+?)s*
'/s*(
/is',
)?s*(.+?)s*
'/s*(
/is',
)?s*(.+?)s*
'/s*(
/is',
)?s*(.+?)s*
'/[li]s*(.+?)s*[/li]/is',
));
$rpl_ary = array_merge($rpl_ary,array(
'
',
'1',
'1',
'1',
'1',
'1',
'1',
'
'
'
'
'2',
'2',
'2',
'2
',
'2
',
'3
',
'3
',
'
));
}
if($this->element){
$reg_ary = array_merge($reg_ary,array(
'/s*(.+?)s*/i',
'/s*(.+?)s*/i',
'/s*(.+?)s*/i',
'/s*(.+?)s*/i',
'/[quote]s*(
)?s*(.+?)s*
/is',
'/[iframe]s*(.+?)s*[/iframe]/is',
'/[iframe=s*(d+?)s*]s*(.+?)s*[/iframe]/is',
));
$rpl_ary = array_merge($rpl_ary,array(
'2 ',
'1 ',
'2 ',
'1 ',
'
',2
'
如果你的浏览器不支持嵌入框,请点这里查看',
'
如果你的浏览器不支持嵌入框,请点这里查看',
));
}
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(
'',
''
));
}
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
highlight_string(trim(strtr($ary[4][$i],array('''=>'>'," "=>" ","
"=>""))));
$tmp .= '
'.$str[$i+1];
'.(trim($ary[3][$i])?trim($ary[3][$i]):'代码片段:').'
'.ob_get_contents().'
ob_end_clean();
}
$str = $tmp;
unset($tmp);
}
Return $str;
}
}
//上面就是完整的UBB函数
//下面是调用方法,
$u=new EncodeQ3boy();
echo $u->ubbEncode($_POST["ok"]);