/*
If reproduced, please indicate the author
Original Author: he Zhiqiang
Improvement: sonymusic[Sonymusic@163.net]
Files: ubb.php
Note: Say is improved, in fact, the core function parse () has been completely rewritten, and the idea is not the same.
But still be inspired by the example of he Zhiqiang, and the test examples and urlcheck and several other functions are also used he zhiqiang procedures, thank you he zhiqiang.
There is no color function yet, but I will join.
If there is any bug or inconvenience in the program, please send me mail.
Thank you!
Improved Features:
The string is UBB encoded, and the class currently supports only a few simple and useful encodings:
1. URL Walks connection
[url] http://phpuser.com/[/url]
/HTTP header may not be required
such as [Url]phpuser.com[/url] is also possible.
2. Email Walks connect
[email] sonymusic@163.net [/email]
3. Picture Walks connection
[img] http://www.phpchina.com/images/logo.gif [/IMG]
As with URL links, the preceding HTTP is not allowed.
4. Textual aspects
[B] Bold word [/b]
[i] Italic word [/i]
[u] underline [/u]
[h1]1 title Word [/h1] ... [h6]6 title Word [/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]
Note the following points:
1. Labels such as url,email,img are not case-sensitive.
2. Tab key is not allowed in the label, but space is allowed.
3. The class is to invoke the Htmlencode,htmlencode4textarea,emailcheck function and the Urlcheck class.
4. The modifications support nesting, but the url,email,img three tags are not allowed to be nested.
Technical information:
Ultimate Bulletin Board
http://www.ultimatebb.com/
What is UBB Code
Http://www.scriptkeeper.com/ubb/ubbcode.html
*/
Include ("urlcheck.php");
Include ("otherfunc.php"); The contents of these two documents are annexed to the last.
Ubbcode class
Class ubbcode{
var $call _time=0;
Table for handling labels and processing functions
var $tags = array (//lowercase label = = Corresponding handler function
' 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 Walks connection Properties
var $attr _url;
URL legality Check Object
var $urlcheck;
function Ubbcode ($attr _url) {
$this->attr_url = ". $attr _url;
$this->urlcheck = new Urlcheck ();
}
Parsing of $str with UBB encoding
Function Parse ($STR) {
$this->call_time++;
$parse = ". HTMLEncode ($STR);
$ret = ";
while (true) {
$eregi _ret=eregi ("[[#]{0,1}[[:alnum:]]{1,7}]", $parse, $eregi _arr); Find [XX]
if (! $eregi _ret) {
$ret. = $parse;
Break If not, return
}
$pos = @strpos ($parse, $eregi _arr[0]);
$tag _len=strlen ($eregi _arr[0]) -2;//marker length
$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. "
";
return $parse;//If it cannot be a tag that cannot be nested, return directly
}
$parse 2 = substr ($parse, 0, $pos);//Before marking
$parse = substr ($parse, $pos + $tag _len+2);//after Mark
if (!isset ($this->tags[$tag])) {
echo "$tag _start
";
$ret. = $parse 2. ' ['. $tag _start. '];
continue;//if the tag is not supported
}
Find the closing tag for the corresponding
$eregi _ret=eregi ("[/". $tag. "]", $parse, $eregi _arr);
if (! $eregi _ret) {
$ret. = $parse 2. ' ['. $tag _start. '];
continue;//If there is no end tag for the should
}
$pos =strpos ($parse, $eregi _arr[0]);
$value =substr ($parse, 0, $pos);//This is the content between the starting and ending tags
$tag _end=substr ($parse, $pos +2, $tag _len);
$parse =substr ($parse, $pos + $tag _len+3);//content after the end tag
if ($tag! = "url") and ($tag! = "Email") and ($tag! = "img")) {
$value = $this->parse ($value);
}
$ret. = $parse 2;
Eval (' $ret. = '. $this->tags[$tag]. ' ("'. $tag _start. '", "'. $tag _end. '", "'. $value. '");
}
$this->call_time--;
return $ret;
}
function simple ($start, $end, $value) {
Return ' < ' $start. ' > '. $value. ' ';
}
function URL ($start, $end, $value) {
$trim _value=trim ($value);
if (Strtolower (substr ($trim _value,0,7))! = "/http")
$trim _value= "http://" $trim _value;
if ($this->urlcheck->check ($trim _value)) return ' Attr_url. ' > '. $value. ';
else return ' ['. $start. '] '. $value. ' [/'. $end. '] ';
}
function email ($start, $end, $value) {
if (Emailcheck ($value)) return '. $value. ';
else return ' ['. $start. '] '. $value. ' [/'. $end. '] ';
}
function img ($start, $end, $value) {
$trim _value=trim ($value);
if ((Strtolower (substr ($trim _value,0,7)) = "/http") or ($this->urlcheck->check ($trim _value)))
Return ';
else return ' ['. $start. '] '. $value. ' [/'. $end. '] ';
}
}
Test
Echo '';
Echo ' <title>Test</title>';
Echo '';
Echo ';
if (Isset ($ubb)) {
$ubbcode = new Ubbcode (' target= "_blank");
Echo '. $ubbcode->parse ($ubb);
}
Echo '';
Echo '';
?>
Contents of File urlcheck.php:
urlcheck.php
Class urlcheck{
var $regex = Array (//protocol name (note this must be written in lowercase) = = corresponding Regular expression
' ftp ' = ' $this->ftpurl ',
' File ' = ' $this->fileurl ',
' http ' = ' $this->httpurl ',
' https ' = ' $this->httpurl ',
' Gopher ' = ' $this->gopherurl ',
' News ' = ' $this->newsurl ',
' NNTP ' = ' $this->nntpurl ',
' Telnet ' = ' $this->telneturl ',
' Wais ' = ' $this->waisurl '
);
var $lowalpha;
var $hialpha;
var $alpha;
var $digit;
var $safe;
var $extra;
var $national;
var $punctuation;
var $reserved;
var $hex;
var $escape;
var $unreserved;
var $uchar;
var $xchar;
var $digits;
var $urlpath;
var $password;
var $user;
var $port;
var $hostnumber;
var $alphadigit;
var $toplabel;
var $domainlabel;
var $hostname;
var $host;
var $hostport;
var $login;
Ftp
var $ftptype;
var $fsegment;
var $fpath;
var $ftpurl;
File
var $fileurl;
Http,https
var $search;
var $hsegment;
var $hpath;
var $httpurl;
Gopher
var $gopher _string;
var $selector;
var $gtype;
var $gopherurl;
News
var $article;
var $group;
var $grouppart;
var $newsurl;
Nntp
var $nntpurl;
Telnet
var $telneturl;
Wais
var $wpath;
var $wtype;
var $database;
var $waisdoc;
var $waisindex;
var $waisdatabase;
var $waisurl;
function Check ($url) {
$pos = @strpos ($url, ': ', 1);
if ($pos <1) return false;
$prot = substr ($url, 0, $pos);
if (!isset ($this->regex[$prot])) return false;
Eval (' $regex = '. $this->regex[$prot]. '; ');
Return Ereg (' ^ '. $regex. ' $ ', $url);
}
function Urlcheck () {
$this->lowalpha = ' [A-z] ';
$this->hialpha = ' [A-z] ';
$this->alpha = ' ('. $this->lowalpha. ' | '. $this->hialpha. ') ';
$this->digit = ' [0-9] ';
$this->safe = ' [$.+_-] ';
$this->extra = ' [* () '!,] ';
$this->national = ' ([{}|^~ ']|\[|\]) ';
$this->punctuation = ' [<>#%] ';
$this->reserved = ' [?;/: @&=] ';
$this->hex = ' ('. $this->digit. ' | [a-fa-f]) ';
$this->escape = ' (% '. $this->hex. ') {2}) ';
$this->unreserved = ' ('. $this->alpha. ' | '. $this->digit. ' | '. $this->safe. ' | '. $this->extra. ') ';
$this->uchar = ' ('. $this->unreserved. ' | '. $this->escape. ') ';
$this->xchar = ' ('. $this->unreserved. ' | '. $this->reserved. ' | '. $this->escape. ') ';
$this->digits = ' ('. $this->digit. ') +)';
$this->urlpath = ' ('. $this->xchar. ') *)';
$this->password = ' ($this->uchar. ' | [?; &=] '. ') *)';
$this->user = ' ($this->uchar. ' | [?; &=] '. ') *)';
$this->port = $this->digits;
$this->hostnumber = ' ('. $this->digits. '. $this->digits. $this->digits. $this->digits. ') ';
$this->alphadigit = ' ('. $this->alpha. ' | '. $this->digit. ') ';
$this->toplabel = ' ('. $this->alpha. ' | ('. $this->alpha. ' ('. $this->alphadigit. ' | -) * '. $this->alphadigit. ') ';
$this->domainlabel = ' ('. $this->alphadigit. ' | ('. $this->alphadigit. ' ('. $this->alphadigit. ' | -) * '. $this->alphadigit. ') ';
$this->hostname = ' (('. $this->domainlabel. ') \.) * '. $this->toplabel. ') ';
$this->host = ' ('. $this->hostname. ' | '. $this->hostnumber. ') ';
$this->hostport = ' ('. $this->host. ') (: '. $this->port. ') ';
$this->login = ' (('. $this->user. ') (: '. $this->password. ')? @)?'. $this->hostport. ') ';
$this->ftptype = ' [Aidaid] ';
$this->fsegment = ' ($this->uchar. ' | [?: @&=]) *)';
$this->fpath = ' ('. $this->fsegment. ') (/'. $this->fsegment. ') *)';
$this->ftpurl = ' ([ff][tt][pp]://'. $this->login. ' (/'. $this->fpath. ' (; [TT] [YY] [PP] [ee]= '. $this->ftptype. ')?] ';
$this->fileurl = ' ([ff][ii][ll][ee]://('. $this->host. ' | [LL] [OO] [CC] [AA] [LL] [HH] [OO] [SS] [TT])? /'. $this->fpath. ') ';
$this->search = ' ($this->uchar. ' | [;: @&=]) *)';
$this->hsegment = ' ($this->uchar. ' | [;: @&=]) *)';
$this->hpath = ' ('. $this->hsegment. ') (/'. $this->hsegment. ') *)';
$this->httpurl = ' ([hh][tt][tt][pp][ss]?:/ /'. $this->hostport. ' (/'. $this->hpath. ' ([?]'. $this->search ')?) ';
$this->gopher_string = ' ('. $this->xchar. ') *)';
$this->selector = ' ('. $this->xchar. ') *)';
$this->gtype = $this->xchar;
$this->gopherurl = ' ([gg][oo][pp][hh][ee][rr]://'. $this->hostport. ' (/('. $this->gtype. ') ('. $this->selector. ' (%09 '. $this->search. ' (%09 '. $this->gopher_string. ')?) ';
$this->article = ' ($this->uchar. ' | [;/?:&=]) +@ '. $this->host ') ';
$this->group = ' ('. $this->alpha. ') ('. $this->alpha. ' | '. $this->digit. ' | [-.+_]) *)';
$this->grouppart = ' ([*]|]. $this->group. ' | '. $this->article. ') ';
$this->newsurl = ' ([Nn][ee][ww][ss]: '. $this->grouppart. ') ';
$this->nntpurl = ' ([nn][nn][tt][pp]://'. $this->hostport. ' /'. $this->group. ' (/'. $this->digits. ') ';
$this->telneturl = ' ([tt][ee][ll][nn][ee][tt]://'. $this->login. ' /?)';
$this->wpath = ' ('. $this->uchar. ') *)';
$this->wtype = ' ('. $this->uchar. ') *)';
$this->database = ' ('. $this->uchar. ') *)';
$this->waisdoc = ' ([ww][aa][ii][ss]://'. $this->hostport. ' /'. $this->database. ' /'. $this->wtype. ' /'. $this->wpath. ') ';
$this->waisindex = ' ([ww][aa][ii][ss]://'. $this->hostport. ' /'. $this->database. ' [?]' $this->search. ') ';
$this->waisdatabase = ' ([ww][aa][ii][ss]://'. $this->hostport. ' /'. $this->database. ') ';
$this->waisurl = ' ('. $this->waisdatabase. ' | '. $this->waisindex. ' | '. $this->waisdoc. ') ';
}
}
?>
Contents of File otherfunc.php:
otherfunc.php
function HTMLEncode ($STR) {
$str = (string) $str;
$ret = ";
$len = strlen ($STR);
$NL = false;
for ($i =0; $i < $len; $i + +) {
$CHR = $str [$i];
Switch ($CHR) {
Case ' < ':
$ret. = ' < ';
$NL = false;
Break
Case ' > ':
$ret. = ' > ';
$NL = false;
Break
Case ' "':
$ret. = ' "';
$NL = false;
Break
Case ' & ':
$ret. = ' & ';
$NL = false;
Break
/*
Case ':
$ret. = ';
$NL = false;
Break
*/
Case Chr (9):
$ret. = ';
$NL = false;
Break
Case Chr (10):
if ($NL) $nl = false;
else{
$ret. = '
';
$NL = true;
}
Break
Case Chr (13):
if ($NL) $nl = false;
else{
$ret. = '
';
$NL = true;
}
Break
Default
$ret. = $CHR;
$NL = false;
Break
}
}
return $ret;
}
function Htmlencode4textarea ($STR) {
$str = (string) $str;
$ret = ";
$len = strlen ($STR);
for ($i =0; $i < $len; $i + +) {
$CHR = $str [$i];
Switch ($CHR) {
Case ' < ':
$ret. = ' < ';
Break
Case ' > ':
$ret. = ' > ';
Break
Case ' "':
$ret. = ' "';
Break
Case ' & ':
$ret. = ' & ';
Break
Case ':
$ret. = ';
Break
Case Chr (9):
$ret. = ';
Break
Default
$ret. = $CHR;
Break
}
}
return $ret;
}
function Emailcheck ($email) {
$ret =false;
if (Strstr ($email, ' @ ') && strstr ($email, '. ')) {
if (eregi ("^ ([_a-z0-9]+ ([\._a-z0-9-]+) *) @ ([A-z0-9]{2,} (\.[ A-z0-9-]{2,}) *\. [A-z] {2,3}) $ ", $email)) {
$ret =true;
}
}
return $ret;
}
function Str2url ($path) {
Return Eregi_replace ("%2f", "/", UrlEncode ($path));
}
?>
The above describes the USB interface type an improved UBB class, including the USB interface type content, I hope the PHP tutorial interested in a friend helpful.