TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   BB Code (http://www.talkphp.com/general/1336-bb-code.html)

Jmz 10-24-2007 10:19 PM

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?

Wildhoney 10-24-2007 10:38 PM

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.

bluesaga 10-24-2007 10:45 PM

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;




Jmz 10-25-2007 08:07 AM

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)

bluesaga 10-25-2007 08:30 AM

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 :)

Jmz 10-25-2007 09:16 AM

Thanks :)

I'll test it out now.

Jmz 10-25-2007 09:40 AM

Nope, I'm lost lol :(

What I have at the minute is:
PHP Code:

function BBCode($text){
$BBCode = array("&" => "&amp;",
"<" => "&lt;",
">" => "&gt;",
"[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?

Gurnk 10-25-2007 10:42 AM

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); 


Wildhoney 10-25-2007 11:26 AM

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;




Salathe 10-25-2007 11:35 AM

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);



Jmz 10-25-2007 11:41 AM

Thats great, thanks a lot.

bluesaga 10-25-2007 11:51 AM

Lol, yea sorry i forgot to escape the patterns properly, got confused with the preg_quote....


All times are GMT. The time now is 10:43 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0