DigitCodes

Coding Notes


Follow @afsalrahim
  • PHP Codes
  • Web Design
    • CSS Tricks
  • WordPress
    • WordPress Tips
    • WordPress Themes
    • WordPress Plugins
  • Blogging
You are here: Home / PHP Codes / Create a Simple PHP BBCode Parser function

Create a Simple PHP BBCode Parser function

March 13, 2013 by Afsal Rahim 15 Comments

PHP BBCode Parser

Let us create a Simple BBCode Parser function with PHP.

I guess you all know about BBCodes? BB code is a very simple set of codes (also known as BB tags) which is used to style or format the text displayed. When a text encapsulated with BB code is displayed in the browser, the BB tags are replaced with appropriate HTML tags. BBCodes are widely used in community sites, forums (like  phpBB or vBulletin) and even blogs commenting section.

Here is an example BBCode and its actual output or formatted text.
BBCode: [b]bold text[/b]
Output: <b>bold text</b>

Learn more PHP Codes : Show user information like IP address, useragent with PHP codes

I guess you got the idea now. So now lets make the PHP function for this BBCode Parser.

BBCode Parser PHP function:

The above code will produce an output as shown:
This is bold and this is underlined and this is in italics with a red color

This is a simple PHP BBcode Parser function which use preg_replace() function to replace all the occurrence of BBcode with its corresponding HTML tags.

Filed Under: PHP Codes

Comments

  1. Collizo4sky says

    April 6, 2013 at 1:27 am

    Awesome php tutorial… Looking to learn PHP soon

    Reply
    • Jesse says

      July 19, 2014 at 11:18 pm

      Do not use this code
      This is susceptible to xss attack. Consider the bbcode

      [img]http://pics.ebaystatic.com/aw/pics/logos/logoPayPal_51x14.gif”onclick=”alert()”[/img]

      Reply
      • Aadarsha Paudel says

        June 15, 2016 at 1:25 am

        Unless you treat it with htmlentitles 🙂

        Reply
        • Luka Paunović says

          March 3, 2017 at 6:26 pm

          Dude this is just a FUNCTION, you need to protect your input.
          You may use my function I made

          //prevent sql injection
          function xss($text) {
          $return = trim($text);
          $return = strip_tags($text);
          $return = htmlspecialchars($text, ENT_QUOTES, ‘UTF-8’);
          return $return;
          }

          Reply
          • Luka Paunović says

            March 3, 2017 at 6:28 pm

            One question for developer of BBCODE function.
            May I use your code for my script I made, I’m planning to sell it on envato, if I credit you.

  2. kay says

    March 18, 2014 at 12:40 pm

    nice, how can I add youtbe support?

    Reply
  3. Wronnay says

    May 9, 2014 at 11:40 pm

    Nice tutorial – i’ve already used bbcodes, but this code is much shorter …

    Reply
  4. Antonio says

    November 26, 2014 at 4:29 pm

    Is it mandatory to use $1 inside the replacement (for example) or i can use another name for the variable like $match?

    Reply
  5. Tyler says

    March 18, 2015 at 9:21 pm

    Hey!

    I will make a list…

    [list]
    [*] Content 1
    [*] Content 2
    [/list]

    But [*] doesn’t work? Can anyone help me?

    I have test:
    ‘~\[*\](.*?)~s’
    but this don’t work on the site…

    Thanks everyone!

    Reply
    • php says

      September 3, 2019 at 2:42 am

      For those who are still interested in a solution to this.
      You can use a callback function like this.

      toc = table of contents

      preg_replace_callback(‘~\[toc=(.*?)\](.*?)\[/toc\]~s’, function($string)
      {

      $list = explode(‘;;’,$string[2]);
      $html = ”.$string[1];
      $html .= ”;
      foreach( $list as $key => $val)
      {
      $html .= ”.$val.”;
      }
      $html .= ”;

      return $html;
      }, $text);

      What you get is a beautiful html output for a list with a title. Usage:

      [toc=My Content]1. Introduction;;2. Foobar;;3. Conclusion[/toc]

      Important is, that you seperate your list points via “;;”. Or feel free to use any seperator.

      Reply
  6. Pyraah says

    August 25, 2015 at 1:16 pm

    (sorry for my bad english)

    Problem with [color], which allow other rules like this:
    [color=blue;font-size:32px]text[/color]

    Do you know how to fix this “problem”?

    Thanks for this useful script :-).

    Reply
    • Pyraah says

      August 25, 2015 at 1:54 pm

      Ok fixed, sorry :-).

      ‘~\[color=([#a-z0-9]+)\](.*?)\[/color\]~s’,

      Reply
      • Pyraah says

        September 5, 2015 at 7:03 am

        Ok, last (really) :
        ~\[color=((?:[a-zA-Z]|#[a-fA-F0-9]{3,6})+)\](.*?)\[/color\]~s

        Match letters (blue, red, white…) OR hex, with length check.

        I’ve fixed all xss attacks and i’ll probably publish this on Github.

        Reply
  7. Terry says

    February 26, 2017 at 11:45 pm

    Thanks for your code, it’s awesome. I have a question, how do I use the [url=/local/path] in local path? not http/https links.

    Reply
  8. Kristoffer says

    June 1, 2019 at 5:15 pm

    Nice, thanks! I was thinking I could use the same function for smileys, having $1 replaced with …/$1.png – But I would like to replace the (Kindof) [emoji] $1 [/emoji] with : $1 : but I cant make it work, could you help?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Sponsered

FREE Email Updates

Subscribe to our newsletter to get all the latest updates to your inbox..!



TRENDING

Adding viewport meta tag to make your website ready for mobile

The Making of DigitCodes: Ideas & Codes + WordPress

How to Easily Show Adsense Ads anywhere Inside the Blog Post

How to Add Your Custom Logo to WordPress Login Screen?

Return to top of page

Copyright © 2016 · digitcodes.com · Privacy Policy · Contact Us