Help with regex lazy repetition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Help with regex lazy repetition

Post by anivad »

Currently I have a replace function that goes:

Code: Select all

$epost = eregi_replace('(\[url\])(.+)(\[\/url\])', '<a href="\\2">\\2</a>', $epost);
which basically makes any link between the url tags clickable. However, it currently matches the last possible url end tag there is, resulting in anything in between the first and last links on the page being linked. The solution is apparently to use lazy repetition instead, replacing the (.+) with (.+?), but this doesn't work in eregi_replace; I tried using preg_replace instead, but got an error message saying that it did not recognise the modifier '('.

Help would be greatly appreciated, thanks!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Help with regex lazy repetition

Post by Ambush Commander »

If you are switching to preg_replace, you now need to have some sort of characters wrapping the regex, like /foobar/ or #foobar#. Try adding those!
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: Help with regex lazy repetition

Post by anivad »

Worked great. Thanks!
Post Reply