php - BB to HTML convertor slow loading page -



php - BB to HTML convertor slow loading page -

i write 1 function converting bb codes in html code php. script works fantastically, came across big problem. page load time slow. whole first version made ​​in array much slow loading. version 2 made individual preg_replace , str_replace , less time need load still on big text-s create problem loading.

can help diminish script , accelerate loading?

this script:

function bb2html($bb2html) { /********************************** * generator bb code html * version: 2.5 beta * updated: 19.06.2014. ***********************************/ $bb2html = str_replace('[[[[','**$@$**[[', $bb2html); $bb2html = str_replace(']]]]',']]**@^@**', $bb2html); $bb2html = str_replace('[[[','**$@$**[', $bb2html); $bb2html = str_replace(']]]',']**@^@**', $bb2html); $bb2html = str_replace('[[','**$@$**', $bb2html); $bb2html = str_replace(']]','**@^@**', $bb2html); // lovercase bb $allow='[c, [sp, [br, [url, [center, [left, [right, [news, [div, [span, [font, [b, [bold, [big, [small, [list, [ul, [li, [tt, [ol, [code, [quote, [block, [cite, [u, [i, [em, [p, [s, [h1, [h2, [h3, [h4, [h5, [h6, [color, [size, [img, [email, [mail, [link, [url'; $allowed=array(); foreach(explode(",", $allow) $result){$result=trim($result);$allowed[]=$result;} while ($lowerrcase = strstr($bb2html, '[')) { $k = (strpos($lowerrcase, ']') > (strpos($lowerrcase, ' ')))?' ':']'; $lowerrcase = substr($lowerrcase, 1, strpos($lowerrcase, $k)); $find=strstr($lowerrcase, '['); if(in_array('['.strtolower($find), $allowed)) $bb2html = str_replace('['.$lowerrcase, '**%^%**'.strtolower($lowerrcase), $bb2html); else $bb2html = str_replace('['.$lowerrcase, '**%^%**'.$lowerrcase, $bb2html); } $bb2html=str_replace('**%^%**', '[', $bb2html); // auto close opened tags $tags_to_close = array('[*]','[b]','[i]','[u]','[big]','[ul]','[list]','[ol]','[center]','[left]','[right]','[tt]','[s]','[code]', '[block]','[h1]','[h2]','[h3]','[h4]','[h5]','[h6]','[quote]'); foreach ($tags_to_close $key => $value) { $open = substr_count($bb2html, $value); $close_tag = '[/'.substr($value, 1); while (substr_count($bb2html, $close_tag) < $open){$bb2html.= $close_tag;} } $check_string = preg_replace("/\[\!--(.+)--\]/i","",$bb2html); // back upwards comments! $removers = array('[hr]','[hr2]','[hr3]','[hr4]','[/br]','[/sp]','[/*]','[/c]'); // remove tags $bb2html = str_replace($removers, '', str_replace($removers, '', $check_string)); /* xss clean-up clean against potential xss attacks adapted bitflux xss prevention techniques.. http://blog.bitflux.ch/wiki/xss_prevention */ $bb2html = implode("",explode("\\",$bb2html)); // prepare &entitiy\n; (except named above) $bb2html = preg_replace('#(&\#*\w+)[\x00-\x20]+;#us',"$1;",$bb2html); $bb2html = preg_replace('#(&\#x*)([0-9a-f]+);*#ius',"$1$2;",$bb2html); $bb2html = html_entity_decode($bb2html, ent_compat); //$bb2html = html_entity_decode($bb2html, ent_compat, "utf-8"); // if php capable of :pref: // remove "on" , other unnecessary attributes (we specify them prevent words "one" beingness affected) $bb2html = preg_replace('#(\[[^\]]+[\x00-\x20\"\'])(onabort|onactivate|onafterprint|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|ondragover|ondragstart|ondrop|onerror|onerrorupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmouseout|onmouseover|onmouseup|onmousewheel|onmove|onmoveend|onmovestart|onpaste|onpropertychange|onreadystatechange|onreset|onresize|onresizeend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload|xmlns|datasrc|src|lowsrc|dynsrc)[^\]]*\]#isuu',"$1]", $bb2html); // remove javascript , vbscript... $bb2html = preg_replace('#([a-z]*)[\x00-\x20]*=?[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iuu', '$1=$2nojavascript...', $bb2html); $bb2html = preg_replace('#([a-z]*)[\x00-\x20]*=?([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iuu', '$1=$2novbscript...', $bb2html); // style look hacks. works in buggy ie... (fer fuxake! browser!) $bb2html = preg_replace('#(\[[^\]]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^\]]*>#ius', "$1\]", $bb2html); $bb2html = preg_replace('#(\[[^\]]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^\]]*>#ius', "$1\]", $bb2html); $bb2html = preg_replace('#(\[[^\]]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^\]]*\]>#iuus',"$1\]", $bb2html); // remove namespaced elements.. $bb2html = preg_replace('#\[/*\w+:\w[^\]]*\]#is',"", $bb2html); // fun <tags>... { $oldbb2html = $bb2html; $bb2html = preg_replace('#\[/*(applet|meta|xml|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|base|sourcetext|parsererror)[^[]*\]#is',"",$bb2html); } while ($oldbb2html != $bb2html); // loop through grab tricky doubles // leave no trace.. if (get_magic_quotes_gpc()) $bb2html = addslashes($bb2html); // pre-formatted text $pre = array(); $i = 9999; while ($pre_str = stristr($bb2html, '[pre]')) { $pre_str = substr($pre_str, 0, strpos($pre_str, '[/pre]') + 6); $bb2html = str_replace($pre_str, "***pre_string***$i", $bb2html); $encode = str_replace("\r\n", "\n", str_replace(array('**$@$**', '**@^@**'), array('[[', ']]'), $pre_str)); $pre[$i] = htmlentities(str_replace(array('[pre]','[/pre]'),'', $encode ), ent_noquotes, 'utf-8'); $i++; } // syntax highlighting $color_code = array(); $i = 0; while ($color_code_str = stristr($bb2html, '[ccc]')) { $color_code_str = substr($color_code_str, 0, strpos($color_code_str, '[/ccc]') + 6); $bb2html = str_replace($color_code_str, "***ccc_string***$i", $bb2html); $color_code[$i] = str_replace(array('**$@$**', '**@^@**', "\r\n"), array('[[', ']]', "\n"), $color_code_str); $i++; } // generic entity encode $bb2html = htmlentities($bb2html, ent_noquotes, 'utf-8'); $bb2html = str_replace('&gt;','>', $bb2html); $bb2html = str_replace('&lt;','<', $bb2html); $bb2html = str_replace('[c]','&copy;', $bb2html); $bb2html = str_replace('[sp]','&nbsp;', $bb2html); $bb2html = str_replace("[br]",'<br />', $bb2html); $bb2html = str_replace("\r\n",'<br />', $bb2html); $bb2html = str_replace("\n",'<br />', $bb2html); $bb2html = str_replace('%0d%0a','<br />', $bb2html); $bb2html = str_replace("<br/>","<br />", $bb2html); $bb2html = str_replace("<br />","<br>", $bb2html); // delete url protocols, because added when convert code protected mod $bb2html = str_replace('[url=http://','[url=', $bb2html); $bb2html = str_replace('[url=https://','[url=', $bb2html); $bb2html = str_replace('[url=ftp://','[url=', $bb2html); $bb2html = str_replace('[url=news://','[url=', $bb2html); $bb2html = str_replace('[url]http://','[url]', $bb2html); $bb2html = str_replace('[url]https://','[url]', $bb2html); $bb2html = str_replace('[url]ftp://','[url]', $bb2html); $bb2html = str_replace('[url]news://','[url]', $bb2html); // other tags $bb2html = str_replace('[*]','<li>', $bb2html); $bb2html = str_replace('[/*]','</li>', $bb2html); // global replace closed tags $bb2html = preg_replace('/\[center\](.*?)\[\/center\]/is','<div align="center">$1</div>', $bb2html); $bb2html = preg_replace('/\[left\](.*?)\[\/left\]/is','<div align="left">$1</div>', $bb2html); $bb2html = preg_replace('/\[right\](.*?)\[\/right\]/is','<div align="right">$1</div>', $bb2html); $bb2html = preg_replace('/\[news\](.*?)\[\/news\]/is','<div class="news" id="news">$1</div>', $bb2html); $bb2html = preg_replace('/\[div\](.*?)\[\/div\]/is','<div>$1</div>', $bb2html); $bb2html = preg_replace('/\[span\](.*?)\[\/span\]/is','<span>$1</span>', $bb2html); $bb2html = preg_replace('/\[font\](.*?)\[\/font\]/is','<font>$1</font>', $bb2html); $bb2html = preg_replace('/\[font=(.*?)\](.*?)\[\/font\]/is','<font style="font-family:$1">$2</font>', $bb2html); $bb2html = preg_replace('/\[b\](.*?)\[\/b\]/is','<strong>$1</strong>', $bb2html); $bb2html = preg_replace('/\[bold\](.*?)\[\/bold\]/is','<strong>$1</strong>', $bb2html); $bb2html = preg_replace('/\[strong\](.*?)\[\/strong\]/is','<strong>$1</strong>', $bb2html); $bb2html = preg_replace('/\[big\](.*?)\[\/big\]/is','<big>$1</big>', $bb2html); $bb2html = preg_replace('/\[small\](.*?)\[\/small\]/is','<small>$1</small>', $bb2html); $bb2html = preg_replace('/\[list\](.*?)\[\/list\]/is','<ol>$1</ol>', $bb2html); $bb2html = preg_replace('/\[list=(.*?)\](.*?)\[\/list\]/is','<ol start="$1">$2</ol>', $bb2html); $bb2html = preg_replace('/\[ul\](.*?)\[\/ul\]/is','<ul>$1</ul>', $bb2html); $bb2html = preg_replace('/\[li\](.*?)\[\/li\]/is','<li>$1</li>', $bb2html); $bb2html = preg_replace('/\[tt\](.*?)\[\/tt\]/is','<tt>$1</tt>', $bb2html); $bb2html = preg_replace('/\[code\](.*?)\[\/code\]/is','<code>$1</code>', $bb2html); $bb2html = preg_replace('/\[quote\](.*?)\[\/quote\]/is','<blockquote>$1</blockquote>', $bb2html); $bb2html = preg_replace('/\[block\](.*?)\[\/block\]/is','<blockquote>$1</blockquote>', $bb2html); $bb2html = preg_replace('/\[cite\](.*?)\[\/cite\]/is','<cite>$1</cite>', $bb2html); $bb2html = preg_replace('/\[u\](.*?)\[\/u\]/is','<u>$1</u>', $bb2html); $bb2html = preg_replace('/\[i\](.*?)\[\/i\]/is','<em>$1</em>', $bb2html); $bb2html = preg_replace('/\[em\](.*?)\[\/em\]/is','<em>$1</em>', $bb2html); $bb2html = preg_replace('/\[p\](.*?)\[\/p\]/is','<p>$1</p>', $bb2html); $bb2html = preg_replace('/\[s\](.*?)\[\/s\]/is','<span style="text-decoration:line-through">$1</span>', $bb2html); $bb2html = preg_replace('/\[h1\](.*?)\[\/h1\]/is','<h1>$1</h1>', $bb2html); $bb2html = preg_replace('/\[h2\](.*?)\[\/h2\]/is','<h2>$1</h2>', $bb2html); $bb2html = preg_replace('/\[h3\](.*?)\[\/h3\]/is','<h3>$1</h3>', $bb2html); $bb2html = preg_replace('/\[h4\](.*?)\[\/h4\]/is','<h4>$1</h4>', $bb2html); $bb2html = preg_replace('/\[h5\](.*?)\[\/h5\]/is','<h5>$1</h5>', $bb2html); $bb2html = preg_replace('/\[h6\](.*?)\[\/h6\]/is','<h6>$1</h6>', $bb2html); $bb2html = preg_replace('/\[color=(.*?)\](.*?)\[\/color\]/is','<font style="color:$1;">$2</font>', $bb2html); $bb2html = preg_replace('/\[size=(.*?)\](.*?)\[\/size\]/is','<span style="font-size:$1%;">$2</span>', $bb2html); $bb2html = preg_replace('/\[img\](.*?)\[\/img\]/is','<img src="$1">', $bb2html); $bb2html = preg_replace('/\[img=(.*?)\](.*?)\[\/img\]/is','<img src="$1" title="$2" alt="$2">', $bb2html); $bb2html = preg_replace('/\[email\]([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))\[\/email\]/is','<a href="mailto:$1">$1</a>', $bb2html); $bb2html = preg_replace('/\[mail\]([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))\[\/mail\]/is','<a href="mailto:$1">$1</a>', $bb2html); $bb2html = preg_replace('/\[url\=(.*?)\](.*?)\[\/url\]/is','<a target="_blank" href="http://$1">$2</a>', $bb2html); $bb2html = preg_replace('/\[url\](.*?)\[\/url\]/is','<a target="_blank" href="http://$1">$1</a>', $bb2html); $bb2html = preg_replace('/\[link\=(.*?)\](.*?)\[\/link\]/is','<a target="_blank" href="http://$1">$2</a>', $bb2html); $bb2html = preg_replace('/\[link\](.*?)\[\/link\]/is','<a target="_blank" href="http://$1">$1</a>', $bb2html); if (get_magic_quotes_gpc()) $bb2html = implode("",explode("\\",$bb2html)); // clear trash $bb2html = preg_replace('|[[\/\!]*?[^\[\]]*?]|si', '', $bb2html); // url's, , infini tags.. $bb2html = str_replace('/ >','/>', $bb2html); $bb2html = str_replace('-- >','-->', $bb2html); // xml trash $replace_tags=array( "/\<xml(.+?)\>(.+?)\<\/xml\>/is", "/\<w:(.+?)\>(.+?)\<\/w:(.+?)\>/is", "/\<o:(.+?)\>/is", "/<\?(.*)\? ?>/is", "/\<\!--(.+)--\>/i", "/\<\!--(.+)\>/i", ); $bb2html = preg_replace($replace_tags, "", $bb2html); $replace_tags=false; // create injection visual code $bb2html = str_replace('<br>',"<br>", $bb2html); $bb2html = str_replace('<ol>',"<ol>\r\n", $bb2html); $bb2html = str_replace('</ol>',"\r\n</ol>", $bb2html); $bb2html = str_replace('<ul>',"<ul>\r\n", $bb2html); $bb2html = str_replace('</ul>',"\r\n</ul>", $bb2html); $bb2html = str_replace('<tt>',"<tt>\r\n", $bb2html); $bb2html = str_replace('</tt>',"\r\n</tt>", $bb2html); // re-insert preformatted text blocks.. $cp = count($pre) + 9998; ($i=9999;$i <= $cp;$i++) {$bb2html = str_replace("***pre_string***$i", '<pre>'.$pre[$i].'</pre>', $bb2html);} // re-insert colored code, fix-up output, too, create xhtml strict. $cp = count($color_code) - 1; ($i=0 ; $i <= $cp ; $i++) { $tmp_str = substr($color_code[$i], 5, -6); $tmp_str = highlight_string(implode("",explode("\\",$tmp_str)), true); $tmp_str = str_replace('font color="', 'span style="color:', $tmp_str); $tmp_str = str_replace('font', 'span', $tmp_str); $tmp_str = (get_magic_quotes_gpc()) ? addslashes($tmp_str) : null; $bb2html = str_replace("***ccc_string***$i", $tmp_str, $bb2html); } $bb2html = implode("",explode("\\",$bb2html)); homecoming $bb2html; }

thanks!

there 2 big ones can consider.

consider caching: cache result memcache alternatively conversion on input , store html in database , utilize that.

use pecl extension. http://pecl.php.net/package/bbcode done in c faster implementation.

php html bbcode

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -