PHP preg_replace() question

acabrera

Limp Gawd
Joined
May 23, 2002
Messages
197
*note, whenever you see a { it means a [ and } means ]

I'm messing around trying to make a ubb replacer and am running into a wierd issue (this is my first time playing with preg).

I've been able to successfuly replace the following string with the appropriate html code
Code:
{url=http://www.google.com}Google{/url}

And I'm using this code to replace it
PHP:
$url = "[url=http://www.google.com]Google[/url]";
echo preg_replace("|\[url=(.*)\](.*)\[/url\]|", "<a href=\"\\1\">\\2</a>", $url);

and with that i get <a href="http://www.google.com">Google</a> without a hitch.

Now here's the problem, if I throw another url UBB link into that string, for example
Code:
{url=http://www.google.com}Google{/url} and {url=http://www.yahoo.com}Yahoo{/url}

I only get the following output <a href="http://www.yahoo.com">Yahoo</a>

I thought preg_replace was supposed to go through and replace each time it found the pattern. What am I doing wrong?
 
i believe this has to do with greedy crap. this is rather confusing. you want the 'U' (PCRE_UNGREEDY) modifier (not to mention 'i' to make it case insensitive)
http://us2.php.net/manual/en/pcre.pattern.modifiers.php
http://us2.php.net/manual/en/pcre.pattern.syntax.php
read the parts about greedy-ness

so make it
Code:
echo preg_replace("|{url=(.*)}(.*){/url}|iU", "<a href=\"\1\">\2</a>", $url);
notice the addition of 'iU' after your | delimiter

also, it is usually better to use the code tags instead of the php tags because the color scheme of this forum makes the php tags a bit hard to read at times

...how come you were able to use brackets in your code line without it parsing the url tag? i tried it with both php and code tags, it made it a an actual link with the code tag, but with the php tag, it didn't make a link, but echoes http://
 
Originally posted by tim
i believe this has to do with greedy crap. this is rather confusing. you want the 'U' (PCRE_UNGREEDY) modifier (not to mention 'i' to make it case insensitive)
http://us2.php.net/manual/en/pcre.pattern.modifiers.php
http://us2.php.net/manual/en/pcre.pattern.syntax.php
read the parts about greedy-ness

so make it
Code:
echo preg_replace("|{url=(.*)}(.*){/url}|iU", "<a href=\"\1\">\2</a>", $url);
notice the addition of 'iU' after your | delimiter

also, it is usually better to use the code tags instead of the php tags because the color scheme of this forum makes the php tags a bit hard to read at times

...how come you were able to use brackets in your code line without it parsing the url tag? i tried it with both php and code tags, it made it a an actual link with the code tag, but with the php tag, it didn't make a link, but echoes http://

Thanks for your help, I'm not sure exactly wh my url ubb wasn't replaced on the forum (magic I'm guessing).

I played around with the code that you gave me and I got it to work. Thanks a lot.

here's the final code :

Code:
			echo preg_replace('|\[url=(.*)\](.*)\[/url\]|iU', "<a href=\"\\1\">\\2</a>", $url);
 
Hmm, and I just use the following.

Code:
preg_replace('/\[URL=(.*?)\](.*?)\[\/URL\]/i', '<a href="\\1">\\2</a>', $url);

Never used the 'U' before, just always use (.*?) for my matching.
 
Back
Top