TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-24-2007, 10:19 PM   #1 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default 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?
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-24-2007, 10:38 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-24-2007, 10:45 PM   #3 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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;


bluesaga is offline  
Reply With Quote
Old 10-25-2007, 08:07 AM   #4 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

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)
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 08:30 AM   #5 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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 :)
bluesaga is offline  
Reply With Quote
Old 10-25-2007, 09:16 AM   #6 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Thanks :)

I'll test it out now.
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 09:40 AM   #7 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

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?
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 10:42 AM   #8 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

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); 
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 10-25-2007, 11:26 AM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-25-2007, 11:35 AM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 10-25-2007, 11:41 AM   #11 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Thats great, thanks a lot.
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 10-25-2007, 11:51 AM   #12 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Lol, yea sorry i forgot to escape the patterns properly, got confused with the preg_quote....
bluesaga is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:05 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design