Regular Expression preg_match_all help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Regular Expression preg_match_all help

Post 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.
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

addition

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Plonked into the regex forum ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

cheers

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe some actual examples of what it needs to match would help.
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

sure...

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

thanks

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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! :)
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
ska
Forum Commoner
Posts: 41
Joined: Mon Sep 05, 2005 4:54 pm

Post 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
Post Reply