This is an extendable BBcode parser for php, bbcode parser php, powered by Codexpress. A Parser for BBcode, this article will help you with parsing BBcode and also extend markup to add your own tags are allowed. Supported all major BBcode tags.
Demo Download
Demo Download
I made this parser to solve my problem on a client website, so i decide to share it with you just if you found it here.
ADDING NEW BBCODE TAGS
To add new BBcode tags all you need is the tag name and some attributes to the tag, attributes inside the BBcode Object.
For example you wanted to add a tag call [website] which will change it content into clickable link of its website address, Simply add this to the tag object.
$patern="#\[website]([^\[]*)\[/website\]#i"; $replace='<a href="\\1" target="_blank" rel="nofollow">\\1</a>';
This is how to Implement that into this
<a href="http://www.codexpresslabs.info">http://www.codexpresslabs.info</a>
For more additional modification and other examples refer to bbparser.php file
Index.php
<?php require 'bbparser.php'; if (isset($_POST['post'])) { $content=($_POST['content']); $bb = new bbParser(); echo $bb->getHtml($content); } ?> <div class="row"> <form method="POST" action="" id="postform" name="postform" enctype="multipart/form-data"> <script type="text/javascript"> document.postform.title.focus() </script> </p> <p><b>Message</b>: </p> <div id="editbar" style="display: block;"> <a href="javascript:void(0);" onclick="wrapText("body", "[b]", "[/b]")" title="Bold"> <span class="eb"><img src="./images/smiley/bold.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[i]", "[/i]")" title="Italic"> <span class="eb"><img src="./images/smiley/italicize.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[s]", "[/s]")" title="Strikethrough"> <span class="eb"><img src="./images/smiley/strike.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[left]", "[/left]")" title="Align Left"> <span class="eb"><img src="./images/smiley/left.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[right]", "[/right]")" title="Align Right"> <span class="eb"><img src="./images/smiley/right.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[center]", "[/center]")" title="Align Center"> <span class="eb"><img src="./images/smiley/center.gif"></span> </a> <a href="javascript:void(0);" onclick="addText("body", "[hr]")" title="Horizontal Rule"> <span class="eb"><img src="./images/smiley/hr.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[size=8pt]", "[/size]")" title="Font Size"> <span class="eb"><img src="./images/smiley/size.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[font=Lucida Sans Unicode]", "[/font]")" title="Font Face"> <span class="eb"><img src="./images/smiley/face.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[img]", "[/img]")" title="Insert Image/Picture"> <span class="eb"><img src="./images/smiley/img.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[url]", "[/url]")" title="Insert Hyperlink"> <span class="eb"><img src="./images/smiley/url.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[email]", "[/email]")" title="Insert Email"> <span class="eb"><img src="./images/smiley/email.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[website]", "[/website]")" title="Insert Email"> <span class="eb">Add Website</span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[sub]", "[/sub]")" title="Subscript"> <span class="eb"><img src="./images/smiley/sub.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[sup]", "[/sup]")" title="Superscript"> <span class="eb"><img src="./images/smiley/sup.gif"></span> </a> <a href="javascript:void(0);" onclick="wrapText("body", "[quote]", "[/quote]")" title="Quote"> <span class="eb"><img src="./images/smiley/quote.gif"></span> </a> <a href="javascript:void(0);" onclick="addText("body", " ♡")"><img src="./images/emoticons/heart.png" class="faces" title="hert"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/sick.png[/img]")"><img src="./images/emoticons/sick.png" class="faces" title="sick"> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/wub.png[/img]")"><img src="./images/emoticons/wub.png" class="faces" title="wub"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/ninja.png[/img]")"><img src="./images/emoticons/ninja.png" class="faces" title="ninja"> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/kissing.png[/img]")"><img src="./images/emoticons/kissing.png" class="faces" title="kissing"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/alien.png[/img]")"><img src="./images/emoticons/alien.png" class="faces" title="alien"> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/devil.png[/img]")"><img src="./images/emoticons/devil.png" class="faces" title="devil"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/cool.png[/img]")"><img src="./images/emoticons/cool.png" class="faces" title="cool"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/shocked.png[/img]")"><img src="./images/emoticons/shocked.png" class="faces" title="shocked"> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/sideways.png[/img]")"><img src="./images/emoticons/sideways.png" class="faces" title="sideways"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/silly.png[/img]")"><img src="./images/emoticons/silly.png" class="faces" title="silly"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/ermm.png[/img]")"><img src="./images/emoticons/ermm.png" class="faces" title="ermm"> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/whistling.png[/img]")"><img src="./images/emoticons/whistling.png" class="faces" title="slWhisting"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/wink.png[/img]")"><img src="./images/emoticons/wink.png" class="faces" title="wink"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/sad.png[/img]")"><img src="./images/emoticons/sad.png" class="faces" title="Sad"> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/dizzy.png[/img]")"><img src="./images/emoticons/dizzy.png" class="faces" title="Dizzy"> </a> <a href="javascript:void(0);" onclick="addText("body", "[img]images/emoticons/sleeping.png[/img]")"><img src="./images/emoticons/sleeping.png" class="faces" title="sleeping"> </a> <select onchange="wrapText('body', '[color='+this.options[this.selectedIndex].value+']', '[/color]'); this.selectedIndex = 0;" style="margin-bottom: 1ex;"> <option value="" selected="selected">Change Color</option> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> <option value="purple">Purple</option> <option value="brown">Brown</option> <option value="black">Black</option> <option value="lime">Lime</option> <option value="DeepSkyBlue">Deep Sky Blue</option> <option value="silver">Silver</option> <option value="lightgreen">Light Green</option> </select> </div> <script> document.getElementById("editbar").style.display = 'block'; </script> <textarea rows="12" cols="80" name="content" id="body"></textarea> <p> <input type="submit" name="post" value="Submit" accesskey="s"> </p> </form> </div>
parser.php
Download
<?php
class bbParser{
public function __construct(){}
public function getHtml($str){
$bb[] = "#\[b\](.*?)\[/b\]#si";
$html[] = "<b>\\1</b>";
$bb[] = "#\[i\](.*?)\[/i\]#si";
$html[] = "<i>\\1</i>";
$bb[] = "#\[u\](.*?)\[/u\]#si";
$html[] = "<u>\\1</u>";
$bb[] = "#\[hr\]#si";
$html[] = "<hr>";
$bb[] = "#\[del\](.*?)\[/del\]#si";
$html[] = "<del>\\1</del>";
$bb[] = "#\[code\](.*?)\[/code\]#si";
$html[] = "<code>\\1</code>";
$bb[] = "#\[right\](.*?)\[/right\]#si";
$html[] = "<p align='right'>\\1</p>";
$bb[] = "#\[left\](.*?)\[/left\]#si";
$html[] = "<p align='left'>\\1</p>";
$bb[] = "#\[center\](.*?)\[/center\]#si";
$html[] = "<p align='center'>\\1</p>";
$bb[] = "#\[color=red\](.*?)\[/color\]#si";
$html[] = "<font color='red'>\\1</font>";
$bb[] = "#\[color=green\](.*?)\[/color\]#si";
$html[] = "<font color='green'>\\1</font>";
$bb[] = "#\[color=brown\](.*?)\[/color\]#si";
$html[] = "<font color='brown'>\\1</font>";
$bb[] = "#\[color=blue\](.*?)\[/color\]#si";
$html[] = "<font color='blue'>\\1</font>";
$bb[] = "#\[color=purple\](.*?)\[/color\]#si";
$html[] = "<font color='purple'>\\1</font>";
$bb[] = "#\[color=gray\](.*?)\[/color\]#si";
$html[] = "<font color='gray'>\\1</font>";
$bb[] = "#\[color=black\](.*?)\[/color\]#si";
$html[] = "<font color='black'>\\1</font>";
$bb[] = "#\[color=silver\](.*?)\[/color\]#si";
$html[] = "<font color='silver'>\\1</font>";
$bb[] = "#\[color=lightgreen\](.*?)\[/color\]#si";
$html[] = "<font color='lightgreen'>\\1</font>";
$bb[] = "#\[color=DeepSkyBlue\](.*?)\[/color\]#si";
$html[] = "<font color='DeepSkyBlue'>\\1</font>";
$bb[] = "#\[size=8pt\](.*?)\[/size\]#si";
$html[] = "<font size='20px'>\\1</font>";
$bb[] = "#\[s\](.*?)\[/s\]#si";
$html[] = "<strike>\\1</strike>";
$bb[] = "#\[color=lime\](.*?)\[/color\]#si";
$html[] = "<font color='lime'>\\1</font>";
$bb[] = "#\[quote\](.*?)\[/quote\]#si";
$html[] = "<br/><br/><cite>\\1</cite><br/><br/>";
$bb[] = "#\[sup\](.*?)\[/sup\]#si";
$html[] = "<sup>\\1</sup>";
$bb[] = "#\[sub\](.*?)\[/sub\]#si";
$html[] = "<sub>\\1</sub>";
$str = preg_replace ($bb, $html, $str);
$patern="#\[url]([^\[]*)\[/url\]#i";
$replace='<a href="\\1" target="_blank" rel="nofollow">\\1</a>';
$patern="#\[website]([^\[]*)\[/website\]#i";
$replace='<a href="\\1" target="_blank" rel="nofollow">\\1</a>';
$str=preg_replace($patern, $replace, $str);
$patern="#\[img\]([^\[]*)\[/img\]#i";
$replace='<img src="\\1" alt=""/>';
$str=preg_replace($patern, $replace, $str);
$patern="#\[email\]([^\[]*)\[/email\]#i";
$replace='<a href="mailto:\\1">< \\1 ></a>';
$str=preg_replace($patern, $replace, $str);
$str=nl2br($str);
return $str;
}
}
?>
Download
Hopefully this will be somewhat useful for you. if you have any suggestion or find any bug please let me know through comment box.
Hit me with a comment!