> > How to make BB Codes in php

How to make BB Codes in php

Posted on Wednesday 15 August 2012 | No Comments

This is a tutorial on How to make BB Codes in php, it will be a function, which will replace BBCodes to HTML tags. Lets see how to do the stuff.



BB Codes are codes which work same as HTML and are programmed like HTML tags. Instead of angle brackets of HTML, they have squared brackets like – [b]Bold[/b] or [i]Italics[/i].

We’ll create a php function which would replace the BB Codes to HTML. Like [b][/b] would be converted to <b></b> and all other codes. Let’s see step by step the illustration of the function with explanation.

First of all, we’ll create a function.

<?php

    function BB_Codes($string) {

    }//ends function BB_Codes

?>

We have created a function named BB_Codes with a variable argument – $string. When we call this function to replace with BB Codes
, we’ll put all the text of BB Codes in place of this variable as an argument which would then be replaced by this function to HTML.

<?php

    function BB_Codes($string) {

      $BB_Coded = array(
                 '#[b](.*?)[/b]#',
                 '#[i](.*?)[/i]#',
                 '#[url=(.*?)](.*?)[/url]#'
                 );

  }//ends function BB_Codes

?>

Now that we have created an array of BB Codes which will be replaced. This pattern of the values of array is easy, try to understand this pattern.

Now we'll create another array for HTML tags which will be replaced by the set of above BB Codes.

<?php

    function BB_Codes($string) {

      $BB_Coded = array(
                 '#[b](.*?)[/b]#',
                 '#[i](.*?)[/i]#',
                 '#[url=(.*?)](.*?)[/url]#'
                 );

      $BB_Replace = array(
                 '<b>\\1</b>',
                 '<i>\\1</i>',
                 '<a href="\\1">\\2</a>'
                 );

  }//ends function BB_Codes

?>

Lets understand some special characters that are used in these arrays.
In the first array – $BB_Code, there’s a special set of characters – (.*?), which represents a text string which is put by user.

And in the same way, in the second array – $BB_Replace, there’s a number with two backslashes i.e. \\1 or \\2 which also represent that a string has been put by a user. But both are different as BB Codes and HTML are different.

\\1 is for first string put by user and \\2 is for second string and so on.

Now we have to replace the BB Codes of first array to HTML tags of second array. So, we’ll use preg_replace construct. The special characters used in these arrays are due to this construct which recognizes their pattern and replaces the first values of array to another.

<?php

    function BB_Codes($string) {

      $BB_Code = array(
                 '#[b](.*?)[/b]#',
                 '#[i](.*?)[/i]#',
                 '#[url=(.*?)](.*?)[/url]#'
                 );

      $BB_Replace = array(
                 '<b>1</b>',
                 '<i>1</i>',
                 '<a href="1">2</a>'
                 );

      return preg_replace($BB_Code, $BB_Replace, $string);

  }//ends function BB_Codes

?> 

So the preg_replace construct requires three main arguments i.e. first is from what code we have to replace, second is to what code the first code should be replaced and the third argument is from what source have to perform this action of replacement.
So now you can use this function like this example.

<?php

$value = "This is a [b]Bold[/b] code. This is an [i]Italics[/i] code.
And here goes a [url=http://google.com]Link to Google[/url].";

BB_Codes( $value );

?>

As there are BB Codes in content of $value variable, they’ll be replaced to HTML tags.
Similarly you can make any BB Code with attribution, for eg. - for coloring the text – [color=red]Roses are Red [/color] and so on.

Thanks for reading this article, please inform me in comments if there are errors in the script or you have any doubt or confusion in any part of the script.

Leave a Reply

Powered by Blogger.