ubb
//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',
'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',
'html' => '$this->html',
'quote' => '$this->quote',
'swf' => '$this->swf',
'upload' => '$this->upload'
);
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
\n", $er_arr[0]);
printf ("1. %s
\n", $er_arr[1]);
printf ("2. %s
\n", $er_arr[2]);
printf ("3. %s
\n", $er_arr[3]);
printf ("4. %s
\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-zA-Z0-9#.:/&@|\?,%=_\+\"\']+)?\]", $parse, $eregi_arr);
if(!$eregi_ret)
{
$ret .= $parse;
break; //如果没有,返回
}
/* for Debug
else
{
printf ("$. %s
", $eregi_ret);
printf ("0. %s
", $eregi_arr[0]);
printf ("1. %s
", $eregi_arr[1]);
printf ("2. %s
", $eregi_arr[2]);
printf ("3. %s
", $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]))
{
$ret .= $parse2.'['.$tag_start.']';
continue; //如果是不支持的标记
}
//查找对应的结束标记
$eregi_ret=eregi("\[(/".$tag.")\]", $parse, $eregi_arr);
if(!$eregi_ret)
{
$ret .= $parse2.'['.$tag_start.$tag_param.']';
continue;//没有对应该的结束标记
}
$pos= strpos($parse, $eregi_arr[0]);
$value= substr($parse, 0, $pos); //起止标记之间的内容
$tag_end= $eregi_arr[1];
$parse= substr($parse, $pos + $eregi_ret);//结束标记之后的内容
// 允许嵌套标记,递归分析
if (!(($tag == 'code') or ($tag=="url") or ($tag=="email") or ($tag=="img"))){
$value= $this->parse($value);
}
$ret.= $parse2;
$parseFun= sprintf('$ret .= %s($tag_start, $tag_param, $tag_end, $value);', $this->tags[$tag]);
eval($parseFun);
}
$nest --;
return $ret;
}
/*****************************************************
* 简单替换,类似[b]变为
* 标签内容不便,只是替代括号为
*****************************************************/
function simple($start, $para, $end, $value){
if (strlen($para) > 0)
return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
else
return sprintf("%s", $start, $value, $end);
}
/*****************************************************
* 如下认为合法可以没有“http://”;ftp一定要自己加“ftp://”
* 93611
*
* http://www.fogsun.com
*****************************************************/
function url($start, $para, $end, $value){
$sA= $value;
$sURL= substr(trim($para), 1);
if (strlen($sURL) > 0)
{
if (strlen($value) == 0)
$sA= $sURL;
}
else
{
$sURL= trim($value);
}
$sURL= $this->formatURL($sURL);
if($this->checkURL($sURL))
return "$sA";
else {
return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
}
}
/*****************************************************
* 如下认为合法可以没有“mailto:”头;
* [email=pazee@21cn.com]pazee
*
* pazee@21cn.com
*****************************************************/
function email($start, $para, $end, $value){
$sA= $value;
$sURL= substr(trim($para), 1);
if (strlen($sURL) > 0)
{
if (strlen($value) == 0)
$sA= $sURL;
}
else
{
$sURL= trim($value);
}
//if (strtolower(substr($sURL, 0, 7)) != "mailto:")
$sURL= "mail.php?email=". $sURL;
if($this->emailcheck(substr($sURL, 15)))
return "$sA";
else
return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
}
/*****************************************************
* 显示图片;如下用法认为合法
* [img=www.21cn.com/title.jpg][/img]
*
*****************************************************/
function img($start, $para, $end, $value){
$sURL= substr(trim($para), 1);
if (strlen($sURL) $sURL= trim($value);
//$sURL= $this->formatURL($sURL);
if ($this->checkImgURL($sURL))
return sprintf("", $sURL,$sURL);
else
return sprintf("[%s%s]%s[%s]", $start, $para, $value, $end);
}
/*****************************************************
* 字符串从右向左循环移动
* 无参数
* 等效与html的

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver CS6
視覺化網頁開發工具