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.
Awesome php tutorial… Looking to learn PHP soon
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]
Unless you treat it with htmlentitles 🙂
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;
}
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.
nice, how can I add youtbe support?
Nice tutorial – i’ve already used bbcodes, but this code is much shorter …
Is it mandatory to use $1 inside the replacement (for example) or i can use another name for the variable like $match?
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!
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.
(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 :-).
Ok fixed, sorry :-).
‘~\[color=([#a-z0-9]+)\](.*?)\[/color\]~s’,
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.
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.
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?