UBB parsing class
php / **************************************************************** *************** UBB parser V1.0B ** This interpreter supports UBB's subset (I don't know what to do) * You can expand it yourself * Resolution of any [x = a, b, c] xxx [x = a, b, c] xxx [/ x] structure ** Source code is provided by ZY, and most of the code overwritten most of this idea ** If you have any good suggestions Please contact Pazee@21cn.com* Regular expressions to get Z_Yong@163.com's strong support, thank you * 子 2001/12/31 * http://www.fogsun.com** $ reprint please complete Keep this paragraph ** Instructions * 1. This code does not restrict HTML, only increases the conversion support for UBB-> HTML, * If you need to filter the HTML tag before calling this code, * 2. Before calling the code, please remove the redundant backslash in the conversion content before calling the code. * Otherwise, there may be problems * 3. The space is not allowed in theUBB tab ************************* **************************************************** /
Define ("Sversion", "1.2.1.15 beta");
// UBBCODE Class Class Ubbcode {var $ nest; // Regenerative depth, for debug // Processable label and handler table var $ tags = array ('url' => '$ this-> url', 'email' = > '$ this-> email', 'mail' => '$ this-> email', // For fault tolerance, [Mail] and [Email] equivalent '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', 'CIT E '=>' $ this-> simvert ',' small '=>
'$ this-> Simple', 'Big' => '$ this-> Simple', 'blink' => '$ this-> simple', 'fly' => '$ this-> fly', 'move' => $ this-> Move ',' Glow '=>' $ this-> cssstyle ',' Shadow '=>' $ this-> cssstyle ',' blur '=>' $ this-> cssstyle ',' Wave '=>' $ this-> cssstyle ',' Sub '=>' $ this-> simple ',' sup '=>' $ this-> simple ',' size '=>' $ this-> size ' , 'Face' => '$ this-> face', 'font' => '$ this-> face', // For fault tolerance, [font] and [Face] equivalent 'color' => '$ this- > color '); function ubbcode () {$ this -> $ nest = 0; $ this -> $ SLASTMODIFIED = Sprintf ("% s", Date ("YMJ H: I", getLastMod ()));} / *********************************************************** ************** * E-mail to the user is simple to check, * Check if the user's E-mail string is @ 字 元, * There is an English alphabet or number before the @ 元, after which the number string is there, * only two or three English letters after the last decimal point. * Super@mail.wilson.gs can pass check, super @ mail.wilson can't pass check ************************* *************************************************************************************** / FUNCTION EMAILCHECK ($ STR ) {IF (EREGI ("^ [_ /. 0-9a-z] @ (0-9a-z] /.) [AZ] {2,3} $ ", $ STR)) Return True; Else Return False;}
/ ************************************************** ********************************************************************************************************************* *********************************************************** ***************************** / Function Checkurl ($ STR) {$ BVALIDURL = true; if (EREGI ("([A-Z0-9 -] ([[A-Z0-9 -] /.】 {/ * printf ("0.% s
/ n", $ Er_arr [0]); Printf ("0.% S
/ N", $ Er_arr [0]); Printf ("1.% S
/ N", $ Er_arr [1]); Printf ("2.% S
/ N", $ Er_arr [2]); Printf ("3.% s
/ n ", $ Er_arr [3]); Printf (" 4.% s
/ n ", $ Er_arr [4]); * /} else $ bvalidurl = false; return $ BVALIDURL;} / ** *********************************************************** **************** * The image URL input to the user is simple check, * Currently, it can only easily determine if the end is image file * does not support the dynamic generation by CGI Pictures, such as counter such as ************************************************************** **************************************** / Function CheckimGurl ($ STR) {IF ($ this-> Checkurl ($ STR)) {IF (EREGI ("/. (JPEG | JPG | GIF | BMP | PNG | PCX | TIFF | TGA | LWF) $", $ STR) Else Return False;} else returnaf;
/ ************************************************** ************** * Automatic complement URL section, mainly the protocol prefix, * default is htpp: //, support https: //; ftp: //; Finger: //; Gopher: // Wait * Function does not check the legality of the URL ********************************************** ***************************************************************** / FUNCTION FORMATURL ($ Str) {if (! EREGI ("(ftp | http | https | mms | gopher | finger | bbs | telnet): (|)", $ STR)) $ STR = 'http: ///' $ str; RETURN $ STR;} // Take UBB coding for $ STR Function Parse ($ STR) {$ Nest ; $ PARSE = ''. ($ STR); $ RET = ''; while (true) {// Find [XX] or [XX = XX], but not included [xx =] $ EREGI_RET = EREGI ("/ [([AZ] [A-Z0-9] {0,7}) (= [A-Z0- 9 #.: / @ | /?,% = _ / / "/ '] )? /]" $ Parse, $ estree); if (! $ Estree_ret) {$ RET. = $ PARSE; BREAK If not, return} / * for debug else {printf ("$.% S
", $ EREGI_RET); Printf ("0.% s
", $ EREGI_ARR [0]); Printf ("1. % S
, $ EREGI_ARR [1]); Printf ("2.% S
", $ EREGI_ARR [2]); Printf ("3.% s
", $ EREGI_ARR [3]) } * / $ POS = @STRPOS ($ PARSE, $ EREGI_ARR [0]); // Start location $ tag_start = $ EREGI_ARR [1]; $ tag = strtolower ($ EREGI_ARR [1]); $ tag_param = $ EREGI_ARR [2];
$ PARSE2 = Substr ($ PARSE, 0, $ POS); // Tag ($ PARSE, $ POS $ EREGI_RET); // Mark IF ($ THIS-> Tags [$ TAG ]))) {// echo "$ TAG. @ 133: Not supported"; // for debug $ re. = $ Parse2. '['. $ Tag_start. ']'; Continue; // If Is not supported marking} // Find the corresponding end tag $ EREGI_RET = EREGI ("/[ (((@ tag.") /] ", $ PARSE, $ EREGI_ARR); if (! $ EREGI_RET) {//// Echo ('There is no end tag ". $ rrr); // for debug $ re. = $ parse2.' ['. $ tag_start. $ tag_param.'] '; continue; // No end tag } $ POS = STRPOS ($ PARSE, $ EREGI_ARR [0]); $ value = Substr ($ PARSE, 0, $ POS); // The content between the start tag $ TAG_END = $ EREGI_ARR [1]; $ PARSE = SubStr ($ PARSE, $ POS $ EREGI_RET); // End the content // allows nested tags, recursive analysis if (($ tag == "URL" OR ($ tag == "URL" ) or ($ tag == "email") OR ($ T AG == "IMG")))))) {$ value = $ this-> parse ($ value);} $ re. = $ parse2; $ parsefun = sprintf ('$ RET. =% s ($ Tag_Start, $ Tag_Param, $ TAG_END, $ Value); ', $ THIS-> Tags [$ TAG]); Eval ($ PARSEFUN);} $ Nest -; Return $ Ret;}
/ ************************************************** **** * Simple replacement, similar to [b] becomes * Tag content is inconvenient, just replacing braces <> ******************** *************************************** / Function Simple ($ Start, $ Para, $ End, $ VALUE) {IF ( Strlen ($ Para)> 0) Return Sprintf ("[% S% S]% S [% s]", $ start, $ para, $ value, $ end); Else Return Sprintf ("<% s>% s <% s>, $ start, $ value, $ end);} / ****************************************** ******************** * If you think is legal, you can not "http: //"; FTP must add "ftp: //" * [url = http : //www.fogsun.com] 93611 [/ url] * [URL = http://www.fogsun.com] [/ url] * [url] http://www.fogsun.com [/ url] * *********************************************************** ** / Function URL ($ START, $ Para, $ End, $ Value) {$ SA = $ VALUE; $ SURL = Substr (Trim ($ Para), 1); IF (Strlen ($ SURL)> 0) { IF (Strlen) == 0) $ sa = $ surl;} else {$ surl = trim ($ value);} $ surl = $ this-> formaturl ($ surl); if ($ this-> checkurl ($ SU RL)) RETURN " "; Else {Return Sprintf (" [% s% s]% s [% s] ", $ start, $ para, $ Value, $ END);}}
/ ************************************************** **** * If you think is legal, you can don't "Mailto:" Head; * [email=pazee@21cn.com] paze [/ email] * [email=pazee@21cn.com] [/ email] * [email] Pazee @ 21cn.com [/ email] *************************************************** *********** / Function Email ($ START, $ Para, $ End, $ Value) {$ SA = $ Value; $ SURL = Substr (Trim ($ Para), 1); IF Strlen ($ surl)> 0) {if (Strlen ($ value) == 0) $ sa = $ surl;} else {$ surl = trim ($ value);}} (SUBSTR ($ surl, 0, 7)))))! = "Mailto:") $ surl = "Mailto:" $ surl; if ($ this-> Emailcheck (Substr ($ surl, 7))) Return $ SA "; Else Return Sprintf (" [% S% S]% s [% s] ", $ start, $ para, $ value, $ end);} / ******* ********************************************** * display image; The following use is considered to be legal * [img = www.21cn.com / title.jpg] [/ IMG] * [img] www.21cn.com/title.jpg [/ img] ********** **************************** **************** ($ SURL = SUBSTR (Trim ($ Para), 1); IF (Strlen ($ surl) {$ surl = SUBSTR (TRLEN) <= 0) $ surl = trim ($ Value); $ surl = $ this-> formaturl; if ($ this-> checkimgurl) Return Sprintf (" img>", $ surl); Else Return Sprintf ("[% S% S]% s [% s", $ start, $ para, $ value, $ end);
/ ************************************************** **** * Strings from right to left cycle mobile * No parameters * Equivalent and HTML
/ ************************************************** **** * Character halo effects include Glow, Shadow and Blur * Character Slin Effects [Glow = A, B, C] or [Shadow = A, B, C] * 3 parameters allowed by default * Implement text shadow Special effects, * glow, shadow, blur attributes are colors, width and boundary size * Wave properties are deformed frequency, width and boundary size ***************************** ***************************************** / Function CSSStyle (& $ Start, & $ Para, & $ End, & $ Value) {$ RETS = Sprintf ("[% s% s]% s [% s", $ start, $ para, $ value, $ end); if (Strlen ($ para) == 0) {$ Para = "=,";} If (EREGI ("^ = ([#]? [[: Xdigit:] {6} | [A-Z0-9] *), ([0-9] *), ([0-9] *) ", $ Para, $ Er_arr)) {$ color = ($ Er_arr [1]! =")? $ Er_arr [1]: red; // default color $ width = ($ Er_arr [2]! = "")? $ Er_arr [2]: 400; // default width $ border = ($ Er_arr [3]! = ")? $ Er_arr [3]: 5; // Default Border Switch ($ start) {case "glow": Case "Shadow": $ RETS = Sprintf (" % S font>, $ START, $ COL OR, $ BORDER, $ Width, $ Value; Break; Case "blur"; $ RETS = Sprintf ("% s font>", $ Start, $ Border, $ Color, $ Width, $ Value; Break; Case "Wave": $ color = ($ Er_arr [1]! = "")? $ Er_arr [1]: 4; // Default Color $ Border = ($ Er_arr [3]! = ")? $ Er_arr [3]: 2; // Default Border $ RETS = Sprintf ("
/ "Filter:% S (FREQ =% s, strongth =% s); Width:% s /">% s font> ", $ Start, $ Color, $ Border, $ Width, $ VALUE); Break }}} RETURN $ RETS;} / ************************************************** ************ * Font Color [color = n] xxx [/ color] * n can be #xxxxxx or xxxxxx (6-bit 16) * Red, GREED, Blue, Black, etc. Color retention word also valid * Equivalent and HTML xxx font> * [color] xxxx [/ color] is equivalent to [color = red] ********** *********************************************************** / Function Color ($ Start, $ S) PARA, $ END, $ VALUE) {$ CL = STRTOLOWER (Substr ($ Para, 1)); if ($ CL == ") $ cl =" red "; if (EREGI (" (^ [#]? [[: xdigit:] {6}) | Red | Green | Blue | Yellow | Blue | White | Gray | Brown ", $ CL)) Return Sprintf (" % s font> ", $ cl, $ value); Else Return Sprintf (" [% s% s]% s [% s ", $ start, $ para, $ value, $ end);} / ************************************************** **** * Font size [size = n] xxx [/ size] 1 <= n xxx font> ******* * ******* *************************************************************** / FUNCTION SIZE ($ Start, $ Para, $ End, $ VALUE) {$ SIZE = Substr ($ Para, 1); IF ($ SIZE> = 1 && $ SIZE <= 7 && (Strlen ($ Para)> 1)) Return Sprintf % s font>, $ size, $ value); Else Return Sprintf ("[% s% s]% s [% s", $ Start, $ Para, $ VALUE , $ END);} / ************************************************** **** * Font Name [FACE = N] n font name, no quotation marks * equivalent and HTML xxx font> ************* ****************************************************************************** / Function Face ($ Start, $ Para, $ End, $ value) {$ fn = substr ($ Para, 1); if (! EREGI ("[[: Punct:], $ fn) && strlen ($ para)> 1) {switchper ($ Fn)) {CASE "ST": $ fn = "Song"; Break; Case "HT": $ fn = "black body"; Break; Case "KT": $ fn = "_gb2312"; break; casse FT ": $ fn =" imitation Song _GB2312 "; Break; Case" YY ": $ fn =" young circle "; Break; Case" LS ": $ fn =" 书 "; Break;} Return Sprintf (" % s font> ", $ fn, $ value);} else Return Sprintf (" [% S% S]% s [] ", $ Start, $ Para, $ Value, $ END);} / ************************************************** ************ * Return to the version of the UBB interpreter and final modification date *********************************** ************************************ / Function GetubBVER (& $ SVER, & $ SLASTMOD) {$ Sver = SVERSION; $ SLASTMOD = $ THIS -> $ SLASTMODIFIED; RETURN $ SVER;}} / ************************************************** ************** 2001.12.31 The basic complete UBB label support function increases support for several tags, such as Face, Size, Color, etc. Increase the extension name of the image URL Check, support GIF, BMP, PCX, TIFF, PNG, JPG, JPEG, TGA, LWF files add functions of automatic full URL protocol part, support HTTP, FPT, GOPHER, FINGER, BBS, TELNET, etc. Allow // and // The equivalent, 2002.01.07 modified the display error of [Face =], increase the Chinese character abbreviation 2002.01.08 to improve the [glow] tag function, add the default value, allow the function to have a default increase [shadow] tag , Use methods and [glow], and allow superimposed effect 2002.01.09 to add [Wave] [BLUR] tag function, use method, and [glow] to be similar, and allow superposition effect 2002.01.15 to modify the regular expression matching characters Strings, because [URL = XXXX] may appear = _ "'and other characters, but use [URL] XXXXX [/ URL] There will be no problem, it did not take into account; formaturl added support for MMS protocol, now Media Player has a lot of format; increase version function, you can return to the version of the UBB interpreter and final modification date ************************************** *************************************************** /?> php // Test INCLUDE ("./ubbcode.php"); Printf (" / n"); echo "
/ N version:% s
/ N final revision date:% s
", $ SVER, $ SLASTMOD); Echo "
At this time, http://our93611.51.net/ubb/ubbintro.htm
By writing a rule, everyone can easily write new UBB functions that support flash, MID, WMA and so on.