An Improved UBB class

Source: Internet
Author: User
Tags html encode gopher nntp

<? PHP
/*
If any reprint is found, please indicate the author

Original Author: He Zhiqiang
Improved: sonymusic [sonymusic@163.net]
File: UBB. php
Note: it is an improvement. In fact, the core function parse () has been completely rewritten, and the idea is different.
However, he Zhiqiang is still inspired by the examples, and the test examples include urlcheck and other functions that he Zhiqiang still uses. Program Thank you, He Zhiqiang.
There is no color function yet, but I will add it.
If there are any bugs or inconveniences in the program, please mail me.
Thank you!
Improved functions:
Encode the string by UBB. Currently, this class only supports the following simple and practical encoding:
1. URL Scheme
[Url] http://phpuser.com/[/url]
Http: // header not required
Such as [url] phpuser.com [/url.
2. email reply
[Email] sonymusic@163.net [/Email]
3. Image seek
[Img] http://www.phpchina.com/images/logo.gif [/img]
Same as the URL link, the previous HTTP can also be avoided.
4. Text
[B] Bold words [/B]
[I] italics [/I]
[U] underline [/u]
[H1] heading 1 [/H1]... [H6] heading 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]
Note the following:
1. tags such as URL, email, and IMG are case-insensitive.
2. A tab key is not allowed in a tag, but spaces are allowed.
3. This class calls the htmlencode, htmlencode4textarea, emailcheck function, and urlcheck class.
4. Nesting is supported after modification, but the URL, email, and IMG tags are not allowed to be nested.
Technical materials :
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 content of these two files, which is appended to the end.

// Ubbcode class
Class ubbcode {
VaR $ call_time = 0;
// The table for processing tags and processing functions
VaR $ tags = array (// lower-case tag => corresponding processing 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 ',
'Up' => '$ 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 scheme Property
VaR $ attr_url;
// URL validity check object
VaR $ urlcheck;

Function ubbcode ($ attr_url ){
$ This-> attr_url = ''. $ attr_url;
$ This-> urlcheck = new urlcheck ();
}

// Parse $ STR by UBB Encoding
Function parse ($ Str ){
$ This-> call_time ++;
$ Parse = ''.html encode ($ Str );

$ Ret = '';
While (true ){
$ Eregi_ret = eregi ("[#] {} [[: alnum:] {}]", $ parse, $ eregi_arr); // find [xx]
If (! $ Eregi_ret ){
$ Ret. = $ parse;
Break; // If NO, return
}
$ Pos = @ strpos ($ parse, $ eregi_arr [0]);
$ Tag_len = strlen ($ eregi_arr [0])-2; // mark 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 the tag cannot be nested, return directly
}

$ Parse2 = substr ($ parse, 0, $ POS); // before marking
$ Parse = substr ($ parse, $ POS + $ tag_len + 2); // After marking
If (! Isset ($ this-> tags [$ tag]) {
Echo "$ tag_start <br> ";
$ Ret. = $ parse2. '['. $ tag_start. ']';
Continue; // if the flag is not supported
}

// Find the end mark of the corresponding pair
$ Eregi_ret = eregi ("[/". $ tag. "]", $ parse, $ eregi_arr );
If (! $ Eregi_ret ){
$ Ret. = $ parse2. '['. $ tag_start. ']';
Continue; // if there is no end mark for it
}
$ Pos = strpos ($ parse, $ eregi_arr [0]);
$ Value = substr ($ parse, 0, $ POS); // This is the content between the start and end tags.
$ Tag_end = substr ($ parse, $ POS + 2, $ tag_len );
$ Parse = substr ($ parse, $ POS + $ tag_len + 3); // content after the end mark

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_value = trim ($ value );
If (strtolower (substr ($ trim_value, 0, 7 ))! = "Http ://")
$ Trim_value = "http: //". $ trim_value;
If ($ this-> urlcheck-> check ($ trim_value) return '<a href = "'. $ trim_value. '"'. $ this-> attr_url. '> '. $ value. '</a> ';
Else return '['. $ start. ']'. $ value. '[/'. $ end. ']';
}

Function email ($ start, $ end, $ value ){
If (emailcheck ($ value) return '<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 '<HTML> ';
Echo 'Echo '<body> ';
Echo '<form action = "'. str2url ($ path_info). '" method = "Post"> ';
Echo '<textarea Cols = "100" rows = "10" name = "UBB"> '.html encode4textarea ($ UBB).' </textarea> <br> ';
Echo '<input type = "Submit" value = "Conversion"> ';
Echo '</form> ';

If (isset ($ UBB )){
$ Ubbcode = new ubbcode ('target = "_ blank "');
Echo '<HR>'. $ ubbcode-> parse ($ UBB );
}

Echo '</body> ';
Echo '

?>

File urlcheck. php content:
<? PHP
// Urlcheck. php
Class urlcheck {
VaR $ RegEx = array (// protocol name (note that it must be written in lower case) => Corresponding Regular Expression
'Ftp '=>' $ this-> ftpurl ',
'File' => '$ this-> fileurl ',
'Http' => '$ this-> httpurl ',
'Http' => '$ 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-> Lphadigit. '| -)*'. $ 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 .')';
}
}

?>

File otherfunc. php content:
<? 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.