A UBB PHP class

Source: Internet
Author: User
Tags php class php code trim
/*
Text Encode Class
Write by Q3boy 2003.3.10
Usage
$eq = new Encodeq3boy ($STR); Initialize class
The following is the Ubbencode parameter
$eq->url = true; Enable URL to automatically resolve default false
$eq->html = true; Enable HTML encoding (handles <,>, full-width/half-width spaces, tabs, line breaks) default True
$eq->image = true; Enable Image Label resolution default True
$eq->font = true; Enable font label resolution default True
$eq->element = true; Enable external element resolution default True
$eq->flash = true; Enable Flash resolution default True
$eq->php = true; Enable syntax highlighting default True
Ubbencode parameter End
Echo ($eq->getimageopener ())//output picture automatically scale required JS function
echo $eq->htmlencode (); Output UBB encoded string
echo "echo $eq->ubbencode (); Output UBB encoded string
echo "echo $eq->removehtml (); Output a string that removes an HTML tag
echo "echo $eq->ubbencode (); Output a string that removes the UBB label
Support UBB Tag list:
Picture class:
[img]gif/aaa.gif[/img] Insert Picture
[limg]gif/aaa.gif[/limg] Picture left Wrap
[rimg]gif/aaa.gif[/rimg] picture right around the row
[cimg]gif/aaa.gif[/cimg] Picture Center Wrap
Text Control class:
[BR] Line feed
[B] Bold word [b]
[i] italic word [i]
[u] underline [u]
[s] Delete line [s]
[Sub] text subscript [Sub]
[SUP] text superscript [SUP]
[Left] text is right-aligned
[Right] text right-aligned
[Center] text centered [center]
[Align= (left|center|right)] text alignment [align]
[Size= ([1-6])] text size [size]
(fonts) [Font]
[Color= (text color)] [Color]
[List] unordered list [list]
[List=s] ordered lists [list]
[List= (a|1| I)] (list mode (Abc,123,i II)) [table]
[List= (NUM)] has a sequence table (counting from num) [list]
[Li] List cell items [Li]
External element classes:
[url] link [/url]
[Url= (link)] link text [/url]
[Email] email address [/email]
[Email= (Mailing address)] Description text [/email] mail address
[Quote] reference block [/quote]
[IFRAME] interpolation frame address [/iframe]
[Iframe= (insert frame height)] interpolation frame address [/iframe]
[Swf]flash animation address [/swf]
[swf= width, height]flash animated address [/swf]
Code block:
[Code] [/code]
[PHP] [/php]
[code block name] [/code]
[PHP code block name] [/php]
If you want to use the PHP syntax highlighting, be sure to add at both ends of the code block. > "Label
*/
Class Encodeq3boy {
var $str = "";
var $iconpath = "/image/icon";//Icon file path
var $imagepath = "/upfiles/article";//Picture file default path
var $tagfoot = ' border= ' 1 "onload=" imageload (This), "onclick=" Imageopen (This) style= "Cursor:hand";//Picture file attached properties
var $url = false; URL Automatic parsing
var $html = true; HTML encoding
var $image = true; Parsing the image label
var $font = true; Font label
var $element = true; External elements
var $flash = true; Flash
var $php = true; Syntax highlighting
function Encodeq3boy ($str = ', $imgph = ') {
if ($STR) {
$str = STRTR ($str, Array ("\n\r" => "\ n", "\ r \ n" => "\ n", "\ r" => "\ n", "" => "));
$this->str = $str;
}
if ($imgph) $this->imagepath = $imgph;
}
function Getimageopener () {
Return "<script language=\" javascript\ "type=\" text/javascript\ ">\r\nfunction imageload (img) {\r\nif ( img.width>480) img.width=480;\r\n}\r\nfunction Imageopen (img) {\r\nwindow.open (img.src, ', ' Menubar=no, Scrollbars=yes,width= ' + (screen.width-8) + ', height= ' + (screen.height-74) + ', left=0,top=0 '); \r\n}\r\n</script > ";
}
function removehtml ($str = ' ") {
if (! $str) $str = $this->str;
Return Strip_tags ($STR);
}
function Removeubb ($str = ' ") {
if (! $str) $str = $this->str;
Return Preg_replace ("/\[\/?\w+ (\s+[^\]\s]+) *\s*\]/is", "", $str);
}
function HtmlEncode ($str = ' ") {
if (! $str) $str = $this->str;
$ary = Array (
' < ' => ' &lt; ',
' > ' => ' &gt; ',
"" => "&nbsp;&nbsp;",
"" => "&nbsp;&nbsp;"
);
$str = Preg_replace ("/\n{2,}/s", "N/A", Strtr ($str, $ary));
return Str_replace ("\ n", "\n<br/>", $str);
}
function Ubbencode ($str = ' ") {
if (! $str) $str = $this->str;
$RPL _ary = Array ();
$RPL _ary = Array ();
if ($this->html) $str = $this->htmlencode ($str, true);
$tagfoot = $this->tagfoot;
$icon = $this->iconpath;
$image = $this->imagepath;
if ($this->php) {
Preg_match_all ('/(\n\<br \/\>) *\[(Php|code) \s* (. *?) \]\s* (. +?) \s*\[\/(Php|code) \] (\n\<br \/\>) */is ', $str, $ary);
$str = Preg_split ('/(\n\<br \/\>) *\[(Php|code) \s* (. *?) \]\s* (. +?) \s*\[\/(Php|code) \] (\n\<br \/\>) */is ', $str);
}
if ($this->url) {
$reg _ary = Array_merge ($reg _ary,array (
'/(? <!\]|\=) \s* (\b\w+@ (?: \ W+\.) +\w{2,3}) \b\s* (? <!\[)/I ',
'/(? <!\]|\=) \s* (\b (http|https|ftp): \/\/(\w+\.) +\w+ (\/[\w|&|%| \?| =|\+|\.| -]+) *) \b\s* (? <!\[)/I ',
));
$RPL _ary = Array_merge ($rpl _ary,array (
' [Email]\\1[/email] ',
' [Url]\\1[/url] ',
));
}
if ($this->image) {
$reg _ary = Array_merge ($reg _ary,array (
'/\[img\]\s*http (s?): \ /\/(\s+) \s*\[\/img\]/i ',
'/\[limg\]\s*http (s?): \ /\/(\s+) \s*\[\/limg\]/i ',
'/\[rimg\]\s*http (s?): \ /\/(\s+) \s*\[\/rimg\]/i ',
'/\[cimg\]\s*http (s?): \ /\/(\s+) \s*\[\/cimg\]/i ',
'/\[img\]\s* ([^\/:]+) \s*\[\/img\]/i ',
'/\[limg\]\s* ([^\/:]+) \s*\[\/limg\]/i ',
'/\[rimg\]\s* ([^\/:]+) \s*\[\/rimg\]/i ',
'/\[cimg\]\s* ([^\/:]+) \s*\[\/cimg\]/i ',
'/\[img\]\s* (\s+) \s*\[\/img\]/i ',
'/\[limg\]\s* (\s+) \s*\[\/limg\]/i ',
'/\[rimg\]\s* (\s+) \s*\[\/rimg\]/i ',
'/\[cimg\]\s* (\s+) \s*\[\/cimg\]/i ',
));
$RPL _ary = Array_merge ($rpl _ary,array (
' ',
' ',
' ',
' <div align= ' center ' ></div> ',
' ',
' ',
' ',
' <div align= ' center ' ></div> ',
' ',
' ',
' ',
' <div align= ' center ' ></div> ',
));
}
if ($this->font) {
$reg _ary = Array_merge ($reg _ary,array (
'/\[br\]/i ',
'/\[b\]\s* (. +?) \s*\[\/b\]/is ',
'/\[i\]\s* (. +?) \s*\[\/i\]/is ',
'/\[u\]\s* (. +?) \s*\[\/u\]/is ',
'/\[s\]\s* (. +?) \s*\[\/s\]/is ',
'/\[sub\]\s* (. +?) \s*\[\/sub\]/is ',
'/\[sup\]\s* (. +?) \s*\[\/sup\]/is ',
'/\[left\]\s* (. +?) \s*\[\/left\]/is ',
'/\[right\]\s* (. +?) \s*\[\/right\]/is ',
'/\[center\]\s* (. +?) \s*\[\/center\]/is ',
'/\[align=\s* (left|center|right) \]\s* (. +?) \s*\[\/align\]/is ',
'/\[size=\s* ([\.| \d]) \s*\]\s* (. *?) \s*\[\/size\]/is ',
'/\[font=\s* (. *?) \s*\]\s* (. *?) \s*\[\/font\]/is ',
'/\[color=\s* (. *?) \s*\]\s* (. *?) \s*\[\/color\]/is ',
'/\[list\]\s* (<br \/>)? \s* (. +?) \s*\[\/list\]/is ',
'/\[list=s\]\s* (<br \/>)? \s* (. +?) \s*\[\/list\]/is ',
'/\[list= (a|1| I) \]\\s* (<br \/>) \s* (. +?) \s*\[\/list\]/is ',
'/\[list= (\s+?) \]\s* (<br \/>) \s* (. +?) \s*\[\/list\]/is ',
'/\[li\]\s* (. +?) \s*\[\/li\]/is ',
));
$RPL _ary = Array_merge ($rpl _ary,array (
' <br/> ',
' <b>\\1</b> ',
' <i>\\1</i> ',
' <u>\\1</u> ',
' <s>\\1</s> ',
' <sub>\\1</sub> ',
' <sup>\\1</sup> ',
' <div align= ' left ' >\\1</div> ',
' <div align= ' right ' >\\1</div> ',
' <div align= ' center ' >\\1</div> ',
' <div align= ' \\1 ' >\\2</div> ',
' <font size=\\1pt; ' >\\2</font> ',
' <font face= ' \\1 ' >\\2</font> ',
' <font color= ' \\1 ' >\\2</font> ',
' <ul>\\2</ul> ',
' <ol>\\2</ol> ',
' <ol type= ' \\1 ' >\\3</ol> ',
' <ol start= ' \\1 ' >\\3</ol> ',
' <li>\\1</li> ',
));
}
if ($this->element) {
$reg _ary = Array_merge ($reg _ary,array (
'/\[url=\s* (. +?) \s*\]\s* (. +?) \s*\[\/url\]/i ',
'/\[url]\s* (. +?) \s*\[\/url\]/i ',
'/\[email=\s* (. +?) \s*\]\s* (. +?) \s*\[\/email\]/i ',
'/\[email]\s* (. +?) \s*\[\/email\]/i ',
'/\[quote\]\s* (<br \/>)? \s* (. +?) \s*\[\/quote\]/is ',
'/\[iframe\]\s* (. +?) \s*\[\/iframe\]/is ',
'/\[iframe=\s* (\d+?) \s*\]\s* (. +?) \s*\[\/iframe\]/is ',
));
$RPL _ary = Array_merge ($rpl _ary,array (
"<a href=" \1 "target=" _blank ">\2</a>",
"<a href=" \1 "target=" _blank ">\1</a>",
' <a href= ' mailto:\1 ' >\2</a>",
' <a href= ' mailto:\1 ' >\1</a>",
' <table cellpadding= "0" cellspacing= "0" border= "0" width= "90%" align= "center" style= "border:1px gray solid;" ><tr><td><table width= "100%" cellpadding= "5" cellspacing= "1" border= "0" ><tr><td width = "100%" >\2</td></tr></table></td></tr></table>,
"<iframe src=" \ "name=" IFR1 "frameborder=" 0 "allowtransparency=" true "scrolling=" yes "width=" 100% "height=" 340 " Marginwidth= "0" marginheight= "0" hspace= "0" vspace= "0" >\1</iframe><br><a href= "\1" target= "_blank "> If your browser does not support embedded boxes, click here to view </a>",
' <iframe src= "\2" name= "IFR1" frameborder= "0" allowtransparency= "true" scrolling= "yes" width= "100%" height= "\1" Marginwidth= "0" marginheight= "0" hspace= "0" vspace= "0" >\2</iframe><br><a href= "\2" target= "_blank "> If your browser does not support embedded boxes, click here to view </a>",
));
}
if ($this->flash) {
$reg _ary = Array_merge ($reg _ary,array (
'/\[swf\]\s* (. +?) \s*\[\/swf\]/i ',
'/\[swf= (\d+) \, (\d+) \]\s* (. +?) \s*\[\/swf\]/i '
));
$RPL _ary = Array_merge ($rpl _ary,array (
' <object classid= ' clsid:d27cdb6e-ae6d-11cf-96b8-444553540000 ' codebase= ' http://download.macromedia.com/pub/ shockwave/cabs/flash/swflash.cab#version=6,0,29,0 "><param name=" movie "value=" \1 "/><param name=" Quality "value=" High "/><embed src=" \1 "quality=" High "pluginspage=" http://www.macromedia.com/go/ Getflashplayer "type=" Application/x-shockwave-flash "></embed></object>",
' <object classid= ' clsid:d27cdb6e-ae6d-11cf-96b8-444553540000 ' codebase= ' http://download.macromedia.com/pub/ shockwave/cabs/flash/swflash.cab#version=6,0,29,0 "width=" \1 "height=" \2 "><param name=" movie "value=" \3 "/ ><param name= "Quality" value= "High"/><embed src= "\3" quality= "High" pluginspage= "http://" Www.macromedia.com/go/getflashplayer "type=" Application/x-shockwave-flash "width=" \1 "height=" \2 "></embed" ></object> '
));
}
if (sizeof ($reg _ary) &&sizeof ($rpl _ary)) $str = Preg_replace ($reg _ary, $rpl _ary, $STR);
if ($this->php) {
$tmp = $str [0];
For ($i =0 $i <sizeof ($ary [4]); $i + +) {
Ob_start ();
Highlight_string (Trim (STRTR ($ary [4][$i],array (' &lt; ') => ' < ', ' &gt; ' => ' > ', ' &nbsp; ' => "", "<br/>" =>)));
$tmp. = ' <table border=1 cellpadding= "0" cellspacing= "0" style= "border-collapse:collapse" bordercolor= "#055AA0" Width=95%><tr><td><code> '. (Trim ($ary [3][$i])? Trim ($ary [3][$i]): ' Code fragment: '). ' &LT;/CODE&GT;&LT;BR/><table width= "100%" cellpadding= "0" cellspacing= "0" border= "0" ><tr><td width = "100%" class= "code" > ". Ob_get_contents ()." </td></tr></table></td></tr></table> '. $str [$i +1];
Ob_end_clean ();
}
$str = $tmp;
Unset ($TMP);
}
return $str;
}
}
That's the complete UBB function.
The following is the calling method,
$u =new Encodeq3boy ();
echo $u->ubbencode ($_post["OK");
<form method=post action= "" >
<textarea name= "OK" rows= "6" cols= "></textarea>"
<input type= "Submit" >
</form>

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.