Page 1 of 1

Regular Expression preg_match_all help

Posted: Wed Sep 07, 2005 10:23 am
by ska
Hi there,

I have a regular expression question. I have some code similar to the following:

Code: Select all

<?
preg_match_all ('/path\/toimage\/*([0-9a-zA-Z]{10})*.jpg/Uis', $result, $matches);

for ($i=0; $i<count($matches[0]); $i++) {

// do some stuff with $matches[1][$i] (the image name)

}

?>
This finds the image name which in this case is always 10 characters long. I would quite like to be able to identify this image name no matter what length it is though. How would I go about that? (i.e. *([0-9a-zA-Z]{x})*, where x is any length)

Thanks for any advice.

addition

Posted: Wed Sep 07, 2005 10:47 am
by ska
addition to above:

Actually, what would be even better would be to do a preg_match_all on:

Code: Select all

cgi-bin/do.pl?&res&&(get this part between brackets)',
rather than that image path.

Posted: Wed Sep 07, 2005 10:48 am
by Chris Corbyn
Plonked into the regex forum ;)

Posted: Wed Sep 07, 2005 10:52 am
by Chris Corbyn

Code: Select all

preg_match_all('#cgi-bin/do\.pl\?&res&&(.+)#i', $string, $matches);

//Perhaps this is better
preg_match_all('#cgi-bin/do\.pl\?&res&&([^&]+)#i', $string, $matches);
Any reason not to use a standard preg_match() over preg_match_all() ? :)

EDIT | Ah ok I see you loop over the array returned, fair enough :)

cheers

Posted: Wed Sep 07, 2005 11:29 am
by ska
Thanks for the swift reply. Actually, the ', was included in the string. In context, this might make more sense:

Code: Select all

<a href="javascript:void(window.open('http://www.domainname.com/cgi-bin/do.pl?&res&&(part required is this bit)','','width=690,height=550,scrollbars=yes,resizable=yes'))">
So it's that little id code that I need to grab for each instance of this string on the page (which is consistent apart from that little URL code)

Posted: Wed Sep 07, 2005 11:34 am
by feyd
maybe some actual examples of what it needs to match would help.

sure...

Posted: Wed Sep 07, 2005 11:37 am
by ska
sure:

Code: Select all

<a href="javascript:void(window.open('http://www.domainname.com/cgi-bin/do.pl?&res&& PRP1000528','','width=690,height=550,scrollbars=yes,resizable=yes'))">
I wish I was quicker with regexps, perhaps one day they will start to make sense to me...

Posted: Wed Sep 07, 2005 11:42 am
by feyd

Code: Select all

preg_match_all('#(["\']'.preg_quote('http://www.domainname.com/cgi-bin/do.pl?&res&&','#').'(.*?)\\1',$text,$matches);
your data will be in $matches[2], btw.

thanks

Posted: Wed Sep 07, 2005 11:46 am
by ska
Thanks. I gave it a go and I get an error:

Code: Select all

Warning: preg_match_all(): No ending delimiter '#' found
Any idea?

Posted: Wed Sep 07, 2005 11:48 am
by feyd
:oops:

Code: Select all

preg_match_all('#(["\']'.preg_quote('http://www.domainname.com/cgi-bin/do.pl?&res&&','#').'(.*?)\\1#',$text,$matches);
obviously I didn't test either! :)

Posted: Wed Sep 07, 2005 11:53 am
by ska
Sorry, I get another error...

Code: Select all

Warning: preg_match_all(): Compilation failed: missing ) at offset 70
If I understood what was happening in the regex here, this would make more sense to me. One day...

Posted: Wed Sep 07, 2005 11:57 am
by feyd
grrr.. I could have swore I wrote that closing paren in.....:?

Code: Select all

preg_match_all('#(["\'])'.preg_quote('http://www.domainname.com/cgi-bin/do.pl?&res&&','#').'(.*?)\\1#',$text,$matches);

Posted: Wed Sep 07, 2005 12:09 pm
by ska
Ah, I'm with you. Yes, that works. Just got to make the rest of my function work now.

This will help with future regexp stuff, so thanks d11wtq and feyd