[Nottingham] PHP RegEx woes

Michael Simms michael at linuxgamepublishing.com
Fri Oct 3 18:26:34 UTC 2008


Yeah regex is greedy. What you wanna do is make the regex middle
matching bit be anything that *doesnt* have a [ in it, as I assume this
is for url matching, there wont be a [ in it. That way it will refuse to
match as far as past the start of the next one, so so its only possible
closer can be the closer of the current one. 

On Fri, 2008-10-03 at 18:32 +0100, Christopher Joice wrote:
> I'm trying to do what I think should be a very simple find, crop, and
> pad exercise in php.
> What I want to do is this: 
>      1. Find a string which is surrounded by [[ and ]] 
>      2. replace [[ with <a href="foo.bar.co.uk/ 
>      3. put in the string 
>      4. put in "> 
>      5. put in the string, again, so it shows up in the html output 
>      6. replace the ]] with </a>
> I've got a regex which can do this, BUT it's a greedy little bugger so
> only works if there is only one instance in the text, which just
> doesn't happen.
> 
> $page['content'] = ereg_replace("\[\[(.*)]]",
>      '<a href="'.$site["url"].'/page/\\1">\\1</a>', $page['content']);
> 
> I've tried putting some ?s in there to try and make it lazy, but get
> REG_BADRPT errors, so I must be putting it in the wrong place?
> 
> Any help appreciated, I've been going around in circles trying to work
> this one out, and I've not found anything on the 'net that is of use.
> 
> Thanks,
> Christopher Joice
> -
> http://chrisjoice.co.uk
> -
> "May the forces of evil become confused on the way to your house."
> -- George Carlin
> _______________________________________________
> Nottingham mailing list
> Nottingham at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/nottingham
-- 
Michael Simms - CEO Linux Game Publishing LTD
http://www.linuxgamepublishing.com



More information about the Nottingham mailing list