Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2005, 02:13 AM   PM User | #1
polearm
New to the CF scene

 
Join Date: Nov 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
polearm is an unknown quantity at this point
BBcode with Regular Expressions

PHP Code:
<?php
function bbcode_html($input) {
$bbcode = array(
'/\[b\](.+?)\[\/b\]/i',
'/\[i\](.+?)\[\/i\]/i',
'/\[quote\](.+?)\[\/quote\]/i',
'/\[url=(.+?)\](.+?)\[\/url\]/i'
);

$html = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<blockquote>$1</blockquote>',
'<a href="$1">$2</a>'
);

return 
preg_replace($bbcode$html$input);
}
?>
polearm is offline   Reply With Quote
Old 12-28-2005, 05:14 PM   PM User | #2
zealotgi
New Coder

 
Join Date: Dec 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
zealotgi is an unknown quantity at this point
Although this code works when you write it on one line, it wont work if you do the following...

[ quote ]
hello
world
[ /quote ]

because of the multiple lines.

Does anyone know how to fix this?
zealotgi is offline   Reply With Quote
Old 12-28-2005, 05:28 PM   PM User | #3
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
The dot should also match newlines Youd have to add an "s" to the "i".
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-28-2005, 05:30 PM   PM User | #4
zealotgi
New Coder

 
Join Date: Dec 2005
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
zealotgi is an unknown quantity at this point
What if i wanted to nest quotes (which is usually standard in bbcode)?
zealotgi is offline   Reply With Quote
Old 12-28-2005, 06:00 PM   PM User | #5
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
It should work as long as you don't nest the same bbcode (eg.
Quote:
Quote:
...
Why not?
) which will fail but getting that right is kind of difficult.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.

Last edited by marek_mar; 12-28-2005 at 06:06 PM..
marek_mar is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 AM.


Advertisement
Log in to turn off these ads.