Saturday, 12 November 2016

BBcode Parser Written in PHP

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
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(&quot;body&quot;, &quot;[b]&quot;, &quot;[/b]&quot;)" title="Bold">
                <span class="eb"><img src="./images/smiley/bold.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[i]&quot;, &quot;[/i]&quot;)" title="Italic">
                <span class="eb"><img src="./images/smiley/italicize.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[s]&quot;, &quot;[/s]&quot;)" title="Strikethrough">
                <span class="eb"><img src="./images/smiley/strike.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[left]&quot;, &quot;[/left]&quot;)" title="Align Left">
                <span class="eb"><img src="./images/smiley/left.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[right]&quot;, &quot;[/right]&quot;)" title="Align Right">
                <span class="eb"><img src="./images/smiley/right.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[center]&quot;, &quot;[/center]&quot;)" title="Align Center">
                <span class="eb"><img src="./images/smiley/center.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[hr]&quot;)" title="Horizontal Rule">
                <span class="eb"><img src="./images/smiley/hr.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[size=8pt]&quot;, &quot;[/size]&quot;)" title="Font Size">
                <span class="eb"><img src="./images/smiley/size.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[font=Lucida Sans Unicode]&quot;, &quot;[/font]&quot;)" title="Font Face">
                <span class="eb"><img src="./images/smiley/face.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[img]&quot;, &quot;[/img]&quot;)" title="Insert Image/Picture">
                <span class="eb"><img src="./images/smiley/img.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[url]&quot;, &quot;[/url]&quot;)" title="Insert Hyperlink">
                <span class="eb"><img src="./images/smiley/url.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[email]&quot;, &quot;[/email]&quot;)" title="Insert Email">
                <span class="eb"><img src="./images/smiley/email.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[website]&quot;, &quot;[/website]&quot;)" title="Insert Email">
                <span class="eb">Add Website</span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[sub]&quot;, &quot;[/sub]&quot;)" title="Subscript">
                <span class="eb"><img src="./images/smiley/sub.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[sup]&quot;, &quot;[/sup]&quot;)" title="Superscript">
                <span class="eb"><img src="./images/smiley/sup.gif"></span>
            </a>
            <a href="javascript:void(0);" onclick="wrapText(&quot;body&quot;, &quot;[quote]&quot;, &quot;[/quote]&quot;)" title="Quote">
                <span class="eb"><img src="./images/smiley/quote.gif"></span>
            </a>

            <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot; &#9825;&quot;)"><img src="./images/emoticons/heart.png" class="faces" title="hert">
            </a>
            <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/sick.png[/img]&quot;)"><img src="./images/emoticons/sick.png" class="faces" title="sick">
                <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/wub.png[/img]&quot;)"><img src="./images/emoticons/wub.png" class="faces" title="wub">
                </a>
                <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/ninja.png[/img]&quot;)"><img src="./images/emoticons/ninja.png" class="faces" title="ninja">
                    <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/kissing.png[/img]&quot;)"><img src="./images/emoticons/kissing.png" class="faces" title="kissing">
                    </a>
                    <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/alien.png[/img]&quot;)"><img src="./images/emoticons/alien.png" class="faces" title="alien">
                        <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/devil.png[/img]&quot;)"><img src="./images/emoticons/devil.png" class="faces" title="devil">
                        </a>
                        <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/cool.png[/img]&quot;)"><img src="./images/emoticons/cool.png" class="faces" title="cool">
                        </a>

                        <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/shocked.png[/img]&quot;)"><img src="./images/emoticons/shocked.png" class="faces" title="shocked">
                            <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/sideways.png[/img]&quot;)"><img src="./images/emoticons/sideways.png" class="faces" title="sideways">
                            </a>
                            <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/silly.png[/img]&quot;)"><img src="./images/emoticons/silly.png" class="faces" title="silly">
                            </a>
                            <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/ermm.png[/img]&quot;)"><img src="./images/emoticons/ermm.png" class="faces" title="ermm">
                                <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/whistling.png[/img]&quot;)"><img src="./images/emoticons/whistling.png" class="faces" title="slWhisting">
                                </a>
                                <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/wink.png[/img]&quot;)"><img src="./images/emoticons/wink.png" class="faces" title="wink">
                                </a>
                                <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/sad.png[/img]&quot;)"><img src="./images/emoticons/sad.png" class="faces" title="Sad">
                                    <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/dizzy.png[/img]&quot;)"><img src="./images/emoticons/dizzy.png" class="faces" title="Dizzy">
                                    </a>
                                    <a href="javascript:void(0);" onclick="addText(&quot;body&quot;, &quot;[img]images/emoticons/sleeping.png[/img]&quot;)"><img src="./images/emoticons/sleeping.png" class="faces" title="sleeping">
                                    </a>

                                    <select onchange="wrapText(&#39;body&#39;, &#39;[color=&#39;+this.options[this.selectedIndex].value+&#39;]&#39;, &#39;[/color]&#39;); 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


 <?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.


No comments:

Post a Comment

Hit me with a comment!