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 04-07-2008, 12:49 PM   #21 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
Geert is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
Okay, I tried my own [url], and it messed up if there are two url bbcodes in the place.
Same trick. Make .* lazy: .*?
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 04-07-2008, 12:51 PM   #22 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Geert View Post
Same trick. Make .* lazy: .*?
Can you tell me more about ".*?" ? Cause it worked perfectly, and I use it though I hardly know what it means >.<
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 04-07-2008, 01:54 PM   #23 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Broken down, it means:

. matches any character except a newline
* matches whatever precedes it (in this case the ".") 0 or more times.
? matches as few times as possible.

So .* acts a lot like a wildcard when you need to match a variety of possible strings, however, it's greedy, so if you're trying to match

(b)text(/b)

in a string that contains

(b)something(/b) and (b)something else(/b) followed by (b)this(/b)

because it's greedy, it will match from the first (b) all the way down to the last (/b). Make it lazy, and .* only matches as little as possible till it finds something that'll complete the pattern, such as the first (/b) instead of the last one.

For the sake of this post, pretend that all ('s and )'s are ['s and ]'s, because I'm apparently too retarded to figure out how to display them without having them parsed.
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Orc (04-07-2008)
Old 04-07-2008, 01:57 PM   #24 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
Broken down, it means:

. matches any character except a newline
* matches whatever precedes it (in this case the ".") 0 or more times.
? matches as few times as possible.

So .* acts a lot like a wildcard when you need to match a variety of possible strings, however, it's greedy, so if you're trying to match

(b)text(/b)

in a string that contains

(b)something(/b) and (b)something else(/b) followed by (b)this(/b)

because it's greedy, it will match from the first (b) all the way down to the last (/b). Make it lazy, and .* only matches as little as possible till it finds something that'll complete the pattern, such as the first (/b) instead of the last one.

For the sake of this post, pretend that all ('s and )'s are ['s and ]'s, because I'm apparently too retarded to figure out how to display them without having them parsed.
Thanks!

Hey, if you're trying to replace a whole string, and want one piece of the string to still be in tact, how do you do that?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 04-07-2008, 02:20 PM   #25 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I'm sure you could do it any number of ways, but if I wanted to replace "The quick brown fox jumps over the lazy dog" with "A little brown fox gets eaten by my cat", I might use /^[\w\s]+(brown fox)[\w\s]+$/i with a replacement string of "A little $1 gets eaten by my cat"...
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Orc (04-07-2008)
Old 04-11-2008, 01:22 AM   #26 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Code:
$pattern[0] = "/\[b\](.*?)\[\/b\]/i";
Ain't good enough :)

What If I wanted to hmm check the bold tag like this:

Code:
[ b]
test
[/b ]
That little regex wouldn't check it it'll display it like it is.

Thats why you use the "s" modifier :) to make the "." match new lines also :)

Code:
$pattern[0] = "/\[b\](.*?)\[\/b\]/is";
Now
Code:
[b ]
test
[/b ]
Turns into

Code:
<strong>
test
</strong>
I taught my self something :)

(remove the spaces in the [ b] tags -.-.. stupid code tag is broke lol..)
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 12-09-2008, 06:11 PM   #27 (permalink)
The Contributor
 
Evulness's Avatar
 
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
Evulness is on a distinguished road
Default

Using the tutorial at the begining of this topic, i created the following:

Code:
function bbcode($input){

        $code = array('/\[b\](.*?)\[\/b\]/is',
                       '/\[i\](.*?)\[\/i\]/is',
                       '/\[u\](.*?)\[\/u\]/is',
                       '/\[url=(.*?)\](.+?)\[\/url\]/is',
                       '/\[quote\](.*?)\[\/quote\]/is',
                       '/\[color=(.*?)\](.*?)\[\/color\]/is');

        $replace = array('<strong>$1</strong>',
                      '<i>$1</i>',
                      '<u>$1</u>',
                      '<a href="$1">$2</a>',
                      '<br /><div class="quote">$1</div><br />',
                      '<span style="color: $1;">$2</span>');

        $text = preg_replace($code, $replace, $input);

     return $text;

}
echo bbcode('This is a [b]test[\b].');
It isn't replacing anything. Anyone mind poking around a bit and seeing what the issue is?

Thanks.
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
Send a message via AIM to Evulness
Evulness is offline  
Reply With Quote
Old 12-09-2008, 06:50 PM   #28 (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

Quote:
Originally Posted by Evulness View Post
It isn't replacing anything. Anyone mind poking around a bit and seeing what the issue is?

Thanks.
The closing bbcode tag needs a forward slash, not back. E.g.
PHP Code:
//                             ↓ forward slash
echo bbcode('This is a [b]test[/b]'); 
Salathe is offline  
Reply With Quote
Old 12-09-2008, 07:30 PM   #29 (permalink)
The Contributor
 
Evulness's Avatar
 
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
Evulness is on a distinguished road
Default

The smallest errors are a hard to spot sometimes.

Thank you!
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
Send a message via AIM to Evulness
Evulness is offline  
Reply With Quote
Old 12-09-2008, 11:16 PM   #30 (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 was reading the following proposal on Zend_Filter_Bbcode the other day, and somebody on there, as well as on other similar proposals, mentioned that we shouldn't use regular expressions because it might end up with badly formed XHTML. I didn't quite understand that, so could somebody please elucidate for me.
__________________
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 12-10-2008, 02:43 AM   #31 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

*sigh, it is a little known fact that PHP actually has its own built in functions for parsing bbcode.

> http://us.php.net/bbcode

Was going to make an article on it... oh well, just consider it an early Christmas gift.

Just comes to show always check the manual before trying to do it yourself!
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne 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 04:22 AM.

 
     

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