UBB 解析类
UBB 解析类
<?php
/************************************************************
* UBB 解析器 v1.0b
*
* 该解释器支持的是UBB的子集(标准集我也不知到啥样)
* 需要其他的可以自行扩展,
* 理论上支持任何[x=a,b,c]xxx[/x]结构的解析
*
* 源代码由zy提供,在此思路上重写了大部分代码
*
* 如果你有什么好的建议请联系 pazee@21cn.com
* 正则表达式的用法得到 z_yong@163.com 的大力支持,在此表示感谢
* 耙子 2001/12/31
* http://www.fogsun.com
*
* $ 转载请完整保留此段文字 $
*
* 使用说明
* 1.本代码未对html进行任何限制,仅增加了对UBB-〉Html的转换支持,
* 如果需要请在调用本代码前自行过滤html标签,
* 2.调用代码前请用 stripslashes 函数去掉转换内容中的冗余反斜杠,
* 否则结果可能会出现问题
* 3.UBB 标签中不允许出现空格
*************************************************************/
define ("sVersion", "1.2.1.15 beta");
//ubbcode类
class ubbcode
{
var $nest; // 递归深度,for debug
//可处理标签及处理函数表
var $tags = array(
'url' => '$this->url',
'email' => '$this->email',
'mail' => '$this->email', // 为了容错,[mail]和等效
'img' => '$this->img',
'b' => '$this->simple',
'i' => '$this->simple',
'u' => '$this->simple',
'tt' => '$this->simple',
's' => '$this->simple',
'strike' => '$this->simple',
'h1' => '$this->simple',
'h2' => '$this->simple',
'h3' => '$this->simple',
'h4' => '$this->simple',
'h5' => '$this->simple',
'h6' => '$this->simple',
'sup' => '$this->simple',
'sub' => '$this->simple',
'em' => '$this->simple',
'strong' => '$this->simple',
'code' => '$this->simple',
'samp' => '$this->simple',
'kbd' => '$this->simple',
'var' => '$this->simple',
'dfn' => '$this->simple',
'cite' => '$this->simple',
'small' => '$this->simple',
'big' => '$this->simple',
'blink' => '$this->simple',
'fly' => '$this->fly',
'move' => '$this->move',
'glow' => '$this->CSSStyle',
'shadow' => '$this->CSSStyle',
'blur' => '$this->CSSStyle',
'wave' => '$this->CSSStyle',
'sub' => '$this->simple',
'sup' => '$this->simple',
'size' => '$this->size',
'face' => '$this->face',
'font' => '$this->face', // 为了容错,[font]和[face]等效
'color' => '$this->color'
);
function ubbcode()
{
$this->$nest= 0;
$this->$sLastModified= sprintf("%s", date("Y-m-j H:i", getlastmod()));
}
/***********************************************************************
* 对使用者输入的 E-Mail 作简单的检查,
* 检查使用者的 E-Mail 字串是否有 @ 字元,
* 在 @ 字元前有英文字母或数字,在之后有数节字串,
* 最后的小数点后只能有二个或三个英文字母。
* super@mail.wilson.gs 就可以通过检查,super@mail.wilson 就不能通过检查
************************************************************************/
function emailcheck($str)
{
if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $str))
return true;
else
return false;
}
/***********************************************************************
* 对使用者输入的 URL 作简单的检查,
* 目前只能简单判断,不能自动检查fpt,finger等
************************************************************************/
function checkURL($str)
{
$bValidURL= true;
if (eregi("([a-z0-9-]+([\.][a-z0-9\-]+)+)", $str, $er_arr))
{
/*
printf ("0. %s <br>\n", $er_arr[0]);
printf ("1. %s <br>\n", $er_arr[1]);
printf ("2. %s <br>\n", $er_arr[2]);
printf ("3. %s <br>\n", $er_arr[3]);
printf ("4. %s <br>\n", $er_arr[4]);
*/
}
else
$bValidURL= false;
return $bValidURL;
}
/***********************************************************************
* 对使用者输入的 图片URL 作简单的检查,
* 目前只能简单判断结尾是否为图片文件
* 不支持由CGI动态生成的图片,比如计数器这类的
************************************************************************/
function checkImgURL($str)
{
if ($this->checkURL($str)) {
if(eregi("\.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$", $str))
return true;
else
return false;
}
else
return false;
}
/***********************************************************************
* 自动补全URL部分,主要是协议前缀,
* 默认是htpp://,支持https://;ftp://;finger://;gopher://等
* 函数并不对URL的合法性作检查
************************************************************************/
function formatURL($str)
{
if (!eregi("^(ftp|http|https|mms|gopher|finger|bbs|telnet):(\/\/|\\\\)", $str))
$str= 'http://'.$str;
return $str;
}
//对$str进行UBB编码解析
function parse($str)
{
$nest ++;
$parse = ''.($str);
$ret = '';
while(true){
//查找[xx] 或者[xx=xx] , 但不包括[xx=]
$eregi_ret=eregi("\[([a-z][a-z0-9]{0,7})(=[a-z0-9#.:/&@|\?,%=_\+\"\']+)?\]", $parse, $eregi_arr);
if(!$eregi_ret)
{
$ret .= $parse;
break; //如果没有,返回
}
/* for Debug
else
{
printf ("$. %s<br>", $eregi_ret);
printf ("0. %s<br>", $eregi_arr[0]);
printf ("1. %s<br>", $eregi_arr[1]);
printf ("2. %s<br>", $eregi_arr[2]);
printf ("3. %s<br>", $eregi_arr[3]);
}
*/
$pos = @strpos($parse, $eregi_arr[0]); // 起始位置
$tag_start= $eregi_arr[1];
$tag= strtolower($eregi_arr[1]);
$tag_param= $eregi_arr[2];
$parse2 = substr($parse, 0, $pos);//标记之前
$parse = substr($parse, $pos + $eregi_ret);//标记之后
if(!isset($this->tags[$tag]))
{
// echo "$tag.@133:不支持的标记<br>"; // for debug
$ret .= $parse2.'['.$tag_start.']';
continue; //如果是不支持的标记
}
//查找对应的结束标记
$eregi_ret=eregi("\[(/".$tag.")\
Tags:UBB,解析

