View Single Post
Old 04-05-2008, 09:47 PM   #1 (permalink)
mortisimus
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: London, UK
Posts: 47
Thanks: 4
mortisimus is on a distinguished road
Default BBCode using Preg_Replace

Ok guys, it's been a while since I've posted here and about a year since I've written any tutorials. I had a look around and couldn't find any tutorials on this so sorry if it's a repost.

Anyway, I was digging around in my old scripts and found a bbcode function so I guess I'll teach you guys if you want to learn it

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.

PHP Code:
function bbcode$input ) {


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.

PHP Code:
//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:
PHP Code:
echo $bbcoded
So the full function is:
PHP Code:
function bbcode$input ){
    
$pattern[0] = "/\[b\](.*?)\[\/b\]/i";
    
$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:

PHP Code:
$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:

PHP Code:
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:

PHP Code:
//URL's
$pattern[0] = "/\[url\=(.*)\](.*)\[\/url\]/i";
$replace[0] = "<a href=\"$1\">$2</a>";
echo 
preg_replace($pattern$replace$subject); 
Thanks for reading,
If you have any questions just ask

Example of Usage

Thanks for all the tips guys!

Last edited by mortisimus : 04-11-2008 at 03:11 PM.
mortisimus is offline  
Reply With Quote
The Following 2 Users Say Thank You to mortisimus For This Useful Post:
Orc (04-06-2008), Wildhoney (04-06-2008)