An improved UBB class

Source: Internet
Author: User
Tags array eval ftp connect gopher nntp strlen trim
<?php
/*
If reproduced, please specify the author

Original Author: He Zhiqiang
Improvement: sonymusic[Sonymusic@163.net]
File: ubb.php
Remark: Say is an improvement, in fact, the core function parse () has been completely rewritten, and the idea is not the same.
However, is still inspired by the He Zhiqiang example, and the test examples are urlcheck, and so on several functions are also used He Zhiqiang procedures, thank He Zhiqiang.
There are no color features yet, but I will join.
If there are any bugs or inconveniences in the program, please send me mail.
Thank you!
Improved functionality:
UBB encoding of strings, which currently supports only a few simple and useful encodings:
1. URL Walks connection
http://phpuser.com/
http://head can not need
Such as http://www.alixixi.com/program/a/phpuser.com is also possible.
2. Email Walks connect
Sonymusic@163.net
3. Picture Walks Connect

As with the URL link, the preceding HTTP can also not.
4. In terms of writing
Bold Word
Italic character
Underline
[h1]1 title [/H1] ... [h6]6 number title [/h6]
[SUP] [/sup]
[Sub] [/sub]
[TT] [/tt]
[S] [/s]
[Strike] [/strike]
[EM] [/em]
[Strong] [/strong]

[Samp] [/samp]
[KBD] [/KBD]
[Var] [/var]
[DFN] [/DFN]
[Cite] [/cite]
[small] [/small]
[Big] [/big]
[Blink] [/blink]
Note the following points:
1. Url,email,img and other labels are not case-insensitive.
2. The TAB key is not allowed in the label, but spaces allow.
3. The class will call the Htmlencode,htmlencode4textarea,emailcheck function and the Urlcheck class.
4. After the modification support nesting, but url,email,img these three tags are not allowed to nest.
Technical data:
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 attached at the end.

Ubbcode class
Class ubbcode{
var $call _time=0;
Table for handling tags 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 Property
var $attr _url;
URL legality Check Object
var $urlcheck;

function Ubbcode ($attr _url) {
$this->attr_url = '. $attr _url;
$this->urlcheck = new Urlcheck ();
}

Analysis of UBB encoding for $STR
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;//tag 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. " <br> ";
return $parse;//If cannot be a tag that cannot be nested, go straight back
}

$parse 2 = substr ($parse, 0, $pos);//before Mark
$parse = substr ($parse, $pos + $tag _len+2);//after tag
if (!isset ($this->tags[$tag])) {
echo "$tag _start<br>";
$ret. = $parse 2. ' ['. $tag _start.] ';
continue;//if it's not a supported tag
}

Find the end 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 beginning and ending marks
$tag _end=substr ($parse, $pos +2, $tag _len);
$parse =substr ($parse, $pos + $tag _len+3);//after end tag content

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. ' </'. $end. ' > ';
}

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 to ' <a href= '. $trim _value. ' " '. $this->attr_url. ' > '. $value. ' </a> ';
else return ' ['. $start. '] '. $value. ' [/'. $end. '] ';
}

function email ($start, $end, $value) {
if (Emailcheck ($value)) return to ' <a href= ' mailto: '. $value. ' " > '. $value. ' </a> ';
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 ' </img> ';
else return ' ['. $start. '] '. $value. ' [/'. $end. '] ';
}
}

Test
Echo ' Echo ' Echo ' <body> ';
Echo ' <form action= '. Str2url ($PATH _info). ' method= ' post ' > ';
Echo ' <textarea cols= ' rows= ' name= ' Ubb ' > '. Htmlencode4textarea ($ubb). ' </textarea><br> ';
Echo ' <input type= "submit" value= "Convert" > ";
Echo ' </form> ';

if (Isset ($ubb)) {
$ubbcode = new Ubbcode (' target= "_blank");
Echo ' }

Echo ' </body> ';
Echo '
?>


Contents of File urlcheck.php:
<?php
urlcheck.php
Class urlcheck{
var $regex = Array (//protocol name (note that you must write lowercase here) => the 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:
<?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. = ' <br> ';
$NL = true;
}
Break
Case Chr (13):
if ($NL) $nl = false;
else{
$ret. = ' <br> ';
$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));
}
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.