NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page [PHP] Simple BBcode Parse Function

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 12-08-2006, 05:51 PM THREAD STARTER               #1 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)

[PHP] Simple BBcode Parse Function


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 Code:
echo 'Message:<br />'.parsebb($text); 
Currently Parses:
????: NamePros.com http://www.namepros.com/code/266965-php-simple-bbcode-parse-function.html
Colors
Fonts
Images
URLs and Emails
Alignment
Font Sizes
Bold, Underline and Italics

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

Enjoy.
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Last edited by Kadenz; 12-08-2006 at 06:46 PM.
Kadenz is offline  
Old 12-08-2006, 06:17 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
PHP Code:
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>",
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
        
"<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);
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
    
$body preg_replace($find$replace$body);
    return 
$body;

I just made it into 2 arrays.
Last edited by Dan; 01-10-2007 at 06:59 PM.
Dan is offline  
Old 12-08-2006, 06:42 PM THREAD STARTER               #3 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)
lol, I forgot about that thanks again Dan the Man.
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Kadenz is offline  
Old 12-11-2006, 11:45 AM   #4 (permalink)
NamePros Regular
 
asgsoft's Avatar
Join Date: Sep 2005
Location: At Home
Posts: 881
asgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of light
 



How can I get colour coded php bb code?
asgsoft is offline  
Old 12-11-2006, 11:55 AM   #5 (permalink)
Account Suspended
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



Nice im gonna play around with it, looks fun
NetworkTown.Net is offline  
Old 12-11-2006, 12:03 PM THREAD STARTER               #6 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)
Originally Posted by asgsoft
How can I get colour coded php bb code?
The code is already there, just use
PHP Code:
[color=Black]This is Black Text[/color
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Kadenz is offline  
Old 12-11-2006, 12:30 PM   #7 (permalink)
NamePros Regular
 
asgsoft's Avatar
Join Date: Sep 2005
Location: At Home
Posts: 881
asgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of lightasgsoft is a glorious beacon of light
 



but I mean for php code snippest
asgsoft is offline  
Old 12-11-2006, 12:56 PM THREAD STARTER               #8 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)
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.
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Kadenz is offline  
Old 12-11-2006, 01:28 PM   #9 (permalink)
Account Suspended
Join Date: Oct 2005
Location: United Kingdom
Posts: 1,554
NetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really niceNetworkTown.Net is just really nice
 



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 i know its not gonna be easy but if i can get it done ill post it here
NetworkTown.Net is offline  
Old 12-11-2006, 03:54 PM THREAD STARTER               #10 (permalink)
Resistance is Futile
 
Kadenz's Avatar
Join Date: Apr 2006
Location: Montreal, Canada
Posts: 1,094
Kadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to allKadenz is a name known to all
 



Wildlife Lou Gehrig's Disease (ALS)
That would be awesome! Thanks.
__________________
Freelance Web Developer
PHP, MySQL, XHTML, CSS, Javascript, jQuery, Wordpress
Portfolio: www.bundy.ca
Kadenz is offline  
Old 01-10-2007, 10:12 AM   #11 (permalink)
Account Suspended
 
Alex.'s Avatar
Join Date: Nov 2006
Location: Uk
Posts: 601
Alex. is on a distinguished road
 


Ethan Allen Fund Third World Education
Im confused, how can i implament this into my php script?
Alex. is offline  
Old 01-10-2007, 02:39 PM   #12 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
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);
Dan is offline  
Old 01-10-2007, 03:15 PM   #13 (permalink)
Account Suspended
 
Alex.'s Avatar
Join Date: Nov 2006
Location: Uk
Posts: 601
Alex. is on a distinguished road
 


Ethan Allen Fund Third World Education
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?
Alex. is offline  
Old 01-10-2007, 03:39 PM   #14 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
Originally Posted by manofgames
ok, thanks.
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
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
Matthew. is offline  
Old 01-10-2007, 03:53 PM   #15 (permalink)
Account Suspended
 
Alex.'s Avatar
Join Date: Nov 2006
Location: Uk
Posts: 601
Alex. is on a distinguished road
 


Ethan Allen Fund Third World Education
ah, i see.
thanks for your help!
Alex. is offline  
Old 01-10-2007, 06:38 PM   #16 (permalink)
New Member
Join Date: Dec 2006
Location: Melbourne, Australia
Posts: 17
Daniel15 is an unknown quantity at this point
 



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.
__________________
Daniel15, Simple Machines Forum (SMF) Customisation Team
My sites: [DanSoft Australia] [Daniel15.com] [Dynamic-Sig.com]
Need help with SMF? Just ask :)
Daniel15 is offline  
Old 01-10-2007, 06:41 PM   #17 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Go to post 2.
Last edited by Dan; 01-10-2007 at 06:59 PM.
Dan is offline  
Old 01-10-2007, 06:48 PM   #18 (permalink)
New Member
Join Date: Dec 2006
Location: Melbourne, Australia
Posts: 17
Daniel15 is an unknown quantity at this point
 



Those little errors are gone, but the two warnings I got are still there...
What version of PHP are you using for testing?
__________________
Daniel15, Simple Machines Forum (SMF) Customisation Team
My sites: [DanSoft Australia] [Daniel15.com] [Dynamic-Sig.com]
Need help with SMF? Just ask :)
Daniel15 is offline  
Old 01-10-2007, 06:49 PM   #19 (permalink)
NamePros Member
Join Date: Oct 2005
Posts: 173
brianmn is on a distinguished road
 



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.
brianmn is offline  
Old 01-10-2007, 06:59 PM   #20 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
I wasn't testing at all..

I just tested this and it works though:
PHP Code:
function parsebb($body) {
    
$find = array(
        
"@\n@",
        
"@[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]@is"
        
"/\[url\=(.+?)\](.+?)\[\/url\]/is",
        
"/\[b\](.+?)\[\/b\]/is"
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
        
"/\[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>",
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
        
"<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;

Dan is offline  
Old 04-27-2007, 12:18 PM   #21 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
PHP Code:
$stuff "[img]http://www.nameprince.com/forum/images/approx/statusicon/forum_new.gif[/img]";
????: NamePros.com http://www.namepros.com/showthread.php?t=266965
echo 
parsebb($stuff); 
doesnt work
unknowngiver is offline  
Old 05-02-2007, 03:38 AM   #22 (permalink)
Senior Member
 
Hitch's Avatar
Join Date: Aug 2005
Location: Uk, South Yorkshire
Posts: 1,228
Hitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to beholdHitch is a splendid one to behold
 


Winner
PHP Programming - May 2007
Animal Rescue Third World Education Find Marrow Donors!
Originally Posted by asgsoft
How can I get colour coded php bb code?
Read up on highlight_string http://uk.php.net/highlight-string
????: NamePros.com http://www.namepros.com/showthread.php?t=266965

Also, the Code Above, may leave you vulnerable to XSS Attacks, apparently.
Hitch is offline  
Old 05-02-2007, 09:29 AM   #23 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
hey the image tag is not working
unknowngiver is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:13 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger