Creating a bbCode parser with preg_replace

Tutorials for php.

Creating a bbCode parser with preg_replace

Postby Mortisimus on Mon Aug 25, 2008 6:42 pm

Hi guys, in this tutorial, I hope to teach you how to use preg_replace in php to create your own bbcode system.

Right, bbcode is basically created by matching a regular expression and then replacing it with something new (see: PHP: preg_replace - Manual).

For running bbcode on a string, we will create a function called bbcode.

Code: Select all
function bbcode( $input ) {
    // code will go here
}


The only parameter, input will be used as the subject for the replacing of regex.

Now we will create an array of strings so that we can match something and replace! To start off simple, we will only create one. The code below goes in your function.

Code: Select all
//the pattern to be matched
$pattern[0] = "/\[b\](.*?)\[\/b\]/is";
//the replacement
$replace[0] = "<strong>$1</strong>";
//the variable for the replace
$bbcoded = preg_replace($pattern, $replace, $input);
//echo it out
echo $bbcoded;


What's going on? Ok, the $pattern variable is an array so, [0] is added on the end to show what part of the array it is, the same applies to the $replace variable.

In the string for the pattern, a / is added at the start and the end of the string to be used as a delimeter to show what you are searching for. By convention, this is a / but it can be any character, note: the delimters at the start and the end MUST be the same.

The \ is used to escape php using these characters as code. In this case, in front of the / and the [ ].

In the pattern variable, you may notice a (.*?), this is a wildcard and is used to match any string that may be placed here. The ? on the end is lazy regex which basiccally means it will match something as little times as possible. The i on the end of the string makes the match case insensitive (Source: delayedinsanity). The s after the i is there to match new lines/line breaks. (Source: Nor).

In the replace variable, you may notice a $1, this is used to capture whatever is inside the first set of parentheses.

The $bbcoded variable is simple the replacement. preg_replace's parameters work like: mixed regex, mixed replacement, mixed subject. in this case, $pattern is the regex, $replace is the replacement and $input is the subject.

The last thing to do is echo out the replacement:

echo $bbcoded;


So the full function is:

Code: Select all
function bbcode( $input ){
    $pattern[0] = "/\[b\](.*?)\[\/b\]/is";
    $replace[0] = "<strong>$1</strong>";
    $bbcoded = preg_replace($pattern, $replace, $input);
    echo $bbcoded;
}


Taking it further
Of course you can put more search and replacements in here because the variables for regex and replacement are arrays. Please note that if you add more in, the $pattern variable arrays will have to be under each other in ascending order and then the $replace variables can be written. For example:

Code: Select all
$pattern[0] = "/blah/";
$pattern[1] = "/blah/";
$pattern[2] = "/blah/";
$pattern[3] = "/blah/";
$replace[0] = "bloo";
$replace[1] = "bloo";
$replace[2] = "bloo";
$replace[3] = "bloo";


If you do not wish to do this, add the following lines before the preg_replace call:

Code: Select all
ksort($pattern);
ksort($replace);
//then preg_replace here


Also, the preg_replace does not have to be assigned to a variable, it can just be echoed out.

Other examples:

Code: Select all
//URL's
$pattern[0] = "/\[url\=(.*)\](.*)\[\/url\]/is";
$replace[0] = "<a href=\"$1\">$2</a>";
echo preg_replace($pattern, $replace, $subject);


Thanks for reading,
If you have any questions just ask

Note: This tutorial was adapted upon my original post from talkphp.com which I did write. Just in case anyone was wondering.
User avatar
Mortisimus
Site Admin
 
Posts: 21
Joined: Mon Aug 25, 2008 6:04 pm

Return to PHP Tutorials

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron