一个改进的UBB类

论坛 期权论坛 脚本     
niminba   2021-5-23 04:27   1750   0
<?php
/*
如有转载,请注明作者

原作者: 何志强
改进: SonyMusic[ sonymusic@163.net ]
文件: ubb.php
备注: 说是改进,其实核心函数parse()已经完全重写了,而且思路也是不一样的。
不过仍是受何志强的例子的启发,而且测试的例子还有URLCHECK等几个函数也是沿用的何志强的程序,谢谢何志强。
目前还没有颜色的功能,但我会加入的。
如果在程序上有什么BUG或不便的地方,请给我MAIL。
谢谢!
改进功能:
对字符串进行UBB编码,该类目前只支持下列几个简单且实用的编码:
1. URL裢接
[url] http://phpuser.com/ [/url]
http://头可以不需要
如[url]phpuser.com[/url]也是可以的。
2. Email裢接
[email] sonymusic@163.net [/email]
3. 图片裢接
[img] http://www.phpchina.com/images/logo.gif [/img]
同URL链接一样,前面的http也可以不要。
4. 文字方面
[b]粗体字[/b]
[i]斜体字[/i]
[u]加下划线[/u]
[h1]1号标题字[/h1] ... [h6]6号标题字[/h6]
[sup][/sup]
[sub][/sub]
[tt][/tt]
[s][/s]
[strike][/strike]
[em][/em]
[strong][/strong]
[code][/code]
[samp][/samp]
[kbd][/kbd]
[var][/var]
[dfn][/dfn]
[cite][/cite]
[small][/small]
[big][/big]
[blink][/blink]
注意以下几点:
1. url,email,img等标签是不分大小写的.
2. 在标签中不允许有TAB键出现,但空格允许。
3. 该类要调用htmlencode,htmlencode4textarea,emailcheck函数和urlcheck类.
4. 修改后支持嵌套,但url,email,img这三个标签不是允许嵌套的。
技术资料:
Ultimate Bulletin Board
http://www.ultimatebb.com/  
What is UBB Code
http://www.scriptkeeper.com/ubb/ubbcode.html  
*/

include("urlcheck.php");
include("otherfunc.php"); //这两个文件的内容,附在最后。

//ubbcode类
class ubbcode{
var $call_time=0;
//可处理标签及处理函数对应表
var $tags = array( //小写的标签 => 对应的处理函数
'url' => '$this->url',
'email' => '$this->email',
'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'
);
//url裢接属性
var $attr_url;
//url合法性检查对象
var $urlcheck;

function ubbcode($attr_url){
$this->attr_url = ''.$attr_url;
$this->urlcheck = new urlcheck();
}

//对$str进行UBB编码解析
function parse($str){
$this->call_time++;
$parse = ''.htmlencode($str);

$ret = '';
while(true){
$eregi_ret=eregi("[[#]{0,1}[[:alnum:]]{1,7}]",$parse,$eregi_arr); //查找[xx]
if(!$eregi_ret){
$ret .= $parse;
break; //如果没有,返回
}
$pos = @strpos ($parse,$eregi_arr[0]);
$tag_len=strlen($eregi_arr[0])-2;//标记长度
$tag_start=substr($eregi_arr[0],1,$tag_len);
$tag=strtolower($tag_start);

if((($tag=="url") or ($tag=="email") or ($tag=="img")) and ($this->call_time>1)){
echo $this->call_time."<br>";
return $parse;//如果不能是不能嵌套的标记,直接返回
}

$parse2 = substr($parse,0,$pos);//标记之前
$parse = substr($parse,$pos+$tag_len+2);//标记之后
if(!isset($this->tags[$tag])){
echo "$tag_start<br>";
$ret .= $parse2.'['.$tag_start.']';
continue;//如果是不支持的标记
}

//查找对对应的结束标记
$eregi_ret=eregi("[/".$tag."]",$parse,$eregi_arr);
if(!$eregi_ret){
$ret .= $parse2.'['.$tag_start.']';
continue;//如果没有对应该的结束标记
}
$pos=strpos($parse,$eregi_arr[0]);
$value=substr($parse,0,$pos);//这是起止标记之间的内容
$tag_end=substr($parse,$pos+2,$tag_len);
$parse=substr($parse,$pos+$tag_len+3);//结束标记之后的内容

if(($tag!="url") and ($tag!="email") and ($tag!="img")){
$value=$this->parse($value);
}

$ret .= $parse2;
eval('$ret .= '.$this->tags[$tag].'("'.$tag_start.'","'.$tag_end.'","'.$value.'");');
}
$this->call_time--;
return $ret;
}

function simple($start,$end,$value){
return '<'.$start.'>'.$value.'</'.$end.'>';
}

function url($start,$end,$value){
$trim_Rs#2wC6Vvrr2wCV6"fs#2wCFrr2wC6VvrF2wC6Vvrs#2wCGGW&rEEEE55GF2wCrF2wCFGF2wC6V&6s##2wCv%rr2wC6"s#2wC6V7Frr2wC6"s#2wCwGRGF2wC6##2wCv'W&rtuTU%%GF2wCrGF2wCwGRr2wC6V7FSr2wC6V&6Sr2wCv%r##2wC'F6rr2wCV6"r2fwCrs#2wCw&rr2wCr2wCr2wCFvBs#2wCw&'Brr2wCw&w2wC'F6s#2wC7W&rTUuu55r2wCw&'Bs##2wCW&rEEGF2wCrF2wCw&rF2wCFvG2##2wCFVWGW&rEETUTUEEGF2wCv##2wCwFrr2wCV6"s#2wCwGRrr2wCV6"s#2wCFF&6Rrr2wCV6"s#2wCv6Fruu55GF2wCrF2wCFF&6Rr2wCwGRr2wCwFs#2wCv6ruu55GF2wCrF2wCFF&6RF2wC6V&6s#2wCv6FF&6Rruu55GF2wCrF2wCFF&6Rs#2wCv7W&rr2wCv6FF&6Rr2wCv6r2wCv6Fs####wC###~Kf&gVyNXh^Z#C#&gV#FVFRG7G"#G"7G&G7G###WBrs#V7G&7G"#fS#"FFfF#"G7G%F#F6F6"#6Rrfs#WBrfs#fS#V#6RrfwCs#WBrfwCs#fS#V#6RrgV#WBrfV#fS#V#6Rrfs#WBrfs#fS#V##6Rrs#WBrfs#fS#V#ff#6R6"#WBrffffs#fS#V#6R6"#FFfS#6W#WBrf'"fwCs#G'VS##V#6R6"2#FFfS#6W#WBrf'"fwCs#G'VS##V#fV#WBF6##fS#V####GW&&WC####FVFSGFWF&VG7G"#G"7G&G7G###WBrs#V7G&7G"#"FFfF#"G7G%F#F6F6"#6Rrfs#WBrfs#V#6RrfwCs#WBrfwCs#V#6RrgV#WBrfV#V#6Rrfs#WBrfs#V#6Rrs#WBrfs#V#6R6"#WBrffffs#V#fV#WBF6##V####GW&&WC###V6FV#WCS#7G'7G"FVtrff7G'7G"FVr#W&VvgV"""BgVFV#WC'VS###GW&&WC###G#'W&GF#GW&&Vv6RgV&bgVgVgVW&FRGF##wC#
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP