Page 1 of 1

Help with regex lazy repetition

Posted: Tue Aug 25, 2009 4:17 am
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!

Re: Help with regex lazy repetition

Posted: Tue Aug 25, 2009 9:35 am
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!

Re: Help with regex lazy repetition

Posted: Tue Aug 25, 2009 9:55 am
by anivad
Worked great. Thanks!