 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
04-07-2008, 12:49 PM
|
#21 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
|
Quote:
Originally Posted by Orc
Okay, I tried my own [url], and it messed up if there are two url bbcodes in the place.
|
Same trick. Make .* lazy: .*?
|
|
|
|
04-07-2008, 12:51 PM
|
#22 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Geert
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
|
|
|
|
04-07-2008, 01:54 PM
|
#23 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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.
|
|
|
|
|
The Following User Says Thank You to delayedinsanity For This Useful Post:
|
|
04-07-2008, 01:57 PM
|
#24 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by delayedinsanity
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
|
|
|
|
04-07-2008, 02:20 PM
|
#25 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
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
|
|
|
|
|
The Following User Says Thank You to delayedinsanity For This Useful Post:
|
|
04-11-2008, 01:22 AM
|
#26 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 282
Thanks: 61
|
Code:
$pattern[0] = "/\[b\](.*?)\[\/b\]/i";
Ain't good enough :)
What If I wanted to hmm check the bold tag like this:
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
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!
|
|
|
|
12-09-2008, 06:11 PM
|
#27 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
|
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
|
|
|
12-09-2008, 06:50 PM
|
#28 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Evulness
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]');
|
|
|
|
12-09-2008, 07:30 PM
|
#29 (permalink)
|
|
The Contributor
Join Date: Apr 2008
Location: Tampa, FL
Posts: 65
Thanks: 6
|
The smallest errors are a hard to spot sometimes.
Thank you!
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
|
|
|
12-09-2008, 11:16 PM
|
#30 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-10-2008, 02:43 AM
|
#31 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
*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!
|
|
|
|
|
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
|
|
|
|