 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
10-24-2007, 10:19 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
BB Code
Hi,
Whats the best way of adding bb code to a variable?
I've been playing about with a function using str_replace but I have also read that using preg_replace is more efficient and safer, is this true?
Is there a better way I haven't heard of?
|
|
|
10-24-2007, 10:38 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
I doubt it being more efficient for a simple BB parser. Safer, definitely, because with regex you can say if it doesn't have a closing tag then ignore it. That way you can ensure every single tag is closed and thus not affecting the rest of your website.
As for the answer, well, I'll let someone else answer that :) ! As I know they are typing as I type this.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
10-24-2007, 10:45 PM
|
#3 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 165
Thanks: 0
|
I think this is what you mean:
PHP Code:
function BBParser($szInput) {
$aBBPattern = array( '[b](.*?)[/b]' => '<b>\\1</b>', '[i](.*?)[/i]' => '<i>\\1</i>' );
foreach( $aBBPattern as $szKey => $szValue ) { $szPattern = '/'. preg_quote( $szKey, '/' ) .'/is'; $szInput = preg_replace( $szPattern, $szValue, $szInput ); }
return $szInput;
}
|
|
|
|
10-25-2007, 08:07 AM
|
#4 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Thanks for that :)
If I used the method above, how would I do things like:
Code:
[ u r l=http://www.google.com]Google[/ u r l ]
(without the spaces)
|
|
|
10-25-2007, 08:30 AM
|
#5 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 165
Thanks: 0
|
You do need to be able to understand preg pattern matching to be able to understand this, but something like:
PHP Code:
function BBParser($szInput) {
$aBBPattern = array( '[b](.*?)[/b]' => '<b>\\1</b>', '[i](.*?)[/i]' => '<i>\\1</i>', '[url=([^\]+?)](.*?)[/url]' => '<a href="\\1">\\2</a>' );
foreach( $aBBPattern as $szKey => $szValue ) { $szPattern = '/'. preg_quote( $szKey, '/' ) .'/is'; $szInput = preg_replace( $szPattern, $szValue, $szInput ); }
return $szInput;
}
Remember this isnt tested, but the theory is there :)
|
|
|
|
10-25-2007, 09:16 AM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Thanks :)
I'll test it out now.
|
|
|
10-25-2007, 09:40 AM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Nope, I'm lost lol :(
What I have at the minute is:
PHP Code:
function BBCode($text){ $BBCode = array("&" => "&", "<" => "<", ">" => ">", "[b]" => "<b>", "[/b]" => "</b>", "[i]" => "<i>", "[/i]" => "</i>", "[u]" => "<u>", "[/u]" => "</u>", "[img]" => "<img src='", "[/img]" => "'>"); $parsedtext = str_replace(array_keys($BBCode), array_values($BBCode), $text); return $parsedtext; }
That works fine but I want to change it to do it your way.
I've tried:
PHP Code:
function BBcode($szInput) {
$aBBPattern = array( '[b](.*?)[/b]' => '<b>\\1</b>', '[i](.*?)[/i]' => '<i>\\1</i>', '[url=([^\]+?)](.*?)[/url]' => '<a href="\\1">\\2</a>' );
foreach( $aBBPattern as $szKey => $szValue ) { $szPattern = '/'. preg_quote( $szKey, '/' ) .'/is'; $szInput = preg_replace( $szPattern, $szValue, $szInput ); }
return $szInput;
}
Although I get no errors with the above code the bb tags aren't replaced, where am I going wrong?
|
|
|
10-25-2007, 10:42 AM
|
#8 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
|
How are you setting up the string you want to parse?
It should look like this I think
PHP Code:
$sting = "[ b ]My BBC[/ b]"; echo BBcode($string);
|
|
|
10-25-2007, 11:26 AM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
I've redone your regex and removed the preg_quote.
PHP Code:
function BBcode($szInput) { $aBBPattern = array( '\[b\](.*?)\[\/b\]' => '<b>\\1</b>', '\[i\](.*?)\[\/i\]' => '<i>\\1</i>', '\[url="([http:\/\/]+[^"]+)"\](.*)\[\/url\]' => '<a href="\\1">\\2</a>' );
foreach( $aBBPattern as $szKey => $szValue ) { $szPattern = '/'. $szKey .'/isU'; $szInput = preg_replace( $szPattern, $szValue, $szInput ); }
return $szInput;
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
10-25-2007, 11:35 AM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
The problem with bluesaga's function is that the patterns are fed into preg_quote which renders them useless for your needs.
An amended function could be something along the lines of:
PHP Code:
function BBcode($szInput) { $aBBPattern = array( '<strong>\\1</strong>' => '#\[b\](.*?)\[/b\]#is', '<em>\\1</em>' => '#\[i\](.*?)\[/i\]#is', '<u>\\1</u>' => '#\[u\](.*?)\[/u\]#is', '<img src="\\1" />' => '#\[img\](.*?)\[/img\]#i', '<a href="\\1">\\1</a>' => '#\[url\](.*?)\[/url\]#i', '<a href="\\1">\\2</a>' => '#\[url=([^\]]+?)\](.*?)\[/url\]#i' ); return preg_replace(array_values($aBBPattern), array_keys($aBBPattern), $szInput); }
Last edited by Salathe : 10-25-2007 at 12:11 PM.
|
|
|
|
10-25-2007, 11:41 AM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
|
Thats great, thanks a lot.
|
|
|
10-25-2007, 11:51 AM
|
#12 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 165
Thanks: 0
|
Lol, yea sorry i forgot to escape the patterns properly, got confused with the preg_quote....
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|