NameSilo

[PHP] Simple BBcode Parse Function

SpaceshipSpaceship
Watch

Kadenz

Resistance is FutileEstablished Member
Impact
29
Here's a quick function I whipped up for a project and thought some people would have some use for it.
Well here it goes, I am working on the list and indent codes.

EDIT: Use Dan's ;) Below

Usage:
PHP:
echo 'Message:<br />'.parsebb($text);

Currently Parses:
Colors
Fonts
Images
URLs and Emails
Alignment
Font Sizes
Bold, Underline and Italics

Also replaces a url (http://something) with a link.

Enjoy. :D
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
function parsebb($body) {
    $find = array(
        "@\n@",
        "@[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]@is", 
        "/\[url\=(.+?)\](.+?)\[\/url\]/is",
        "/\[b\](.+?)\[\/b\]/is", 
        "/\[i\](.+?)\[\/i\]/is", 
        "/\[u\](.+?)\[\/u\]/is", 
        "/\[color\=(.+?)\](.+?)\[\/color\]/is",
        "/\[size\=(.+?)\](.+?)\[\/size\]/is", 
        "/\[font\=(.+?)\](.+?)\[\/font\]/is",
        "/\[center\](.+?)\[\/center\]/is",
        "/\[right\](.+?)\[\/right\]/is",
        "/\[left\](.+?)\[\/left\]/is",
        "/\[img\](.+?)\[\/img\]/is",
        "/\[email\](.+?)\[\/email\]/is"
    );
    $replace = array(
        "<br />",
        "<a href=\"\\0\">\\0</a>",
        "<a href=\"$1\" target=\"_blank\">$2</a>",
        "<strong>$1</strong>",
        "<em>$1</em>",
        "<span style=\"text-decoration:underline;\">$1</span>",
        "<font color=\"$1\">$2</font>",
        "<font size=\"$1\">$2</font>",
        "<span style=\"font-family: $1\">$2</span>",
        "<div style=\"text-align:center;\">$1</div>",
        "<div style=\"text-align:right;\">$1</div>",
        "<div style=\"text-align:left;\">$1</div>",
        "<img src=\"$1\" alt=\"Image\" />",
        "<a href=\"mailto:$1\" target=\"_blank\">$1</a>"
    );
    $body = htmlspecialchars($body);
    $body = preg_replace($find, $replace, $body);
    return $body;
}
I just made it into 2 arrays. ;)
 
Last edited:
1
•••
lol, I forgot about that :hehe: thanks again Dan the Man. :D
 
0
•••
How can I get colour coded php bb code?
 
0
•••
Nice im gonna play around with it, looks fun ;)
 
0
•••
asgsoft said:
How can I get colour coded php bb code?
The code is already there, just use
PHP:
[color=Black]This is Black Text[/color]
 
0
•••
but I mean for php code snippest
 
0
•••
It's the 7th line down in the arrays. <font color=*****>
EDIT: oops, I know what you mean, like the NamePros php bbcode. Well, I have honestly no idea how to do that.
 
0
•••
im gonna see if i can get javascript to work with it so if you click the button it will enter the bb code in for you then click it agian to close the bb code :D i know its not gonna be easy but if i can get it done ill post it here ;)
 
0
•••
That would be awesome! Thanks.
 
0
•••
Im confused, how can i implament this into my php script?
 
0
•••
Copy the script above your current code inside the PHP tags and when you want to convert something from BB Code to HTML, do:
$variable = parsebb($somevariablewithbbcodeinit);
 
0
•••
ok, thanks.
is there a way to convert it back to bbcode? like when you edit a post?

one last thing, will your script format paragraphs?
 
0
•••
manofgames said:
ok, thanks.
is there a way to convert it back to bbcode? like when you edit a post?

one last thing, will your script format paragraphs?

manofgames, You would not convert it in the first place :)

You would insert the unchanged string into a database (for example) and use this function on the output. That way if you wanted to see the bbcode tags again like in a textarea during edit then simply don't use the function when you output it.

And no it won't format paragraphs :)
 
0
•••
ah, i see.
thanks for your help!
 
0
•••
Looks very nice, however, I noticed two (minor) problems:
1. The
Code:
"<a href=\"$1\" target=\"_blank\">$2</a>"
line is missing a comma at the end
2. The
Code:
"/\[font\=(.+?)\](.+?)\[\/font\]/is",
item in the $find array is missing a corresponding item in the $replace array.

After I fixed these two issues, and try to use the script, I get:
Code:
Warning: preg_replace() [function.preg-replace]: Empty regular expression in /var/www/temp/testbb.php on line 38

Warning: preg_replace() [function.preg-replace]: Unknown modifier '+' in /var/www/temp/testbb.php on line 38

I'm using PHP 5.2.0.
 
0
•••
Go to post 2.
 
Last edited:
0
•••
Those little errors are gone, but the two warnings I got are still there...
What version of PHP are you using for testing?
 
0
•••
This is some nice coding, although for doing bbcode on my site I have always stuck with using the bbcode engine from forums....I then simply integrate the bbcode engine that is used in the forums on my site with other areas of the site that I need to parse bbcode.

But then again that requires you to have forums on your site.

Anyway thanks for the code, I'll keep this code handy so that when I need it I can use it.
 
0
•••
I wasn't testing at all.. ;)

I just tested this and it works though:
PHP:
function parsebb($body) {
    $find = array(
        "@\n@",
        "@[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]@is", 
        "/\[url\=(.+?)\](.+?)\[\/url\]/is",
        "/\[b\](.+?)\[\/b\]/is", 
        "/\[i\](.+?)\[\/i\]/is", 
        "/\[u\](.+?)\[\/u\]/is", 
        "/\[color\=(.+?)\](.+?)\[\/color\]/is",
        "/\[size\=(.+?)\](.+?)\[\/size\]/is", 
        "/\[font\=(.+?)\](.+?)\[\/font\]/is",
        "/\[center\](.+?)\[\/center\]/is",
        "/\[right\](.+?)\[\/right\]/is",
        "/\[left\](.+?)\[\/left\]/is",
        "/\[img\](.+?)\[\/img\]/is",
        "/\[email\](.+?)\[\/email\]/is"
    );
    $replace = array(
        "<br />",
        "<a href=\"\\0\">\\0</a>",
        "<a href=\"$1\" target=\"_blank\">$2</a>",
        "<strong>$1</strong>",
        "<em>$1</em>",
        "<span style=\"text-decoration:underline;\">$1</span>",
        "<font color=\"$1\">$2</font>",
        "<font size=\"$1\">$2</font>",
        "<span style=\"font-family: $1\">$2</span>",
        "<div style=\"text-align:center;\">$1</div>",
        "<div style=\"text-align:right;\">$1</div>",
        "<div style=\"text-align:left;\">$1</div>",
        "<img src=\"$1\" alt=\"Image\" />",
        "<a href=\"mailto:$1\" target=\"_blank\">$1</a>"
    );
    $body = htmlspecialchars($body);
    $body = preg_replace($find, $replace, $body);
    return $body;
}
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back