A useful UBB class!

Source: Internet
Author: User
Tags html encode gopher nntp
? Php * If reproduced, please indicate the author of: He Zhiqiang improved: SonyMusic [sonymusic@163.net] File: ubb. php remarks: 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. /*
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, it is still inspired by the examples of He Zhiqiang, and there are several functions such as URLCHECK in the test examples that He Zhiqiang still uses. 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
Http://phpuser.com/
Http: // header not required
Such as phpuser.com.
2. Email Reply
Sonymusic@163.net
3. image seek

Same as the URL link, the previous http can also be avoided.
4. text
Bold Chinese
Italics
Underline
1 heading word... 6 heading word


[Tt] [/tt]
[S] [/s]

[Em] [/em]
[Strong] [/strong]
[Code] [/code]
[Samp] [/samp]
[Kbd] [/kbd]
[Var] [/var]
[Dfn] [/dfn]
[Cite] [/cite]



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 ("\ [#] {0, 1} [[: alnum:] {1, 7} \]", $ parse, $ eregi_arr ); // search for [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 ."
";
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
";
$ 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 .' ';
}

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' Test';
Echo'';
Echo '';

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

Echo'';
Echo'';

?>

File urlcheck. php content
// 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-> 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 .')';
}
}

?>


File otherfunc. php content
// 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 ));
}
?>

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.