Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
ska
Forum Commoner
Posts: 41 Joined: Mon Sep 05, 2005 4:54 pm
Post
by ska » Wed Sep 07, 2005 10:23 am
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
Post
by ska » Wed Sep 07, 2005 10:47 am
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.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Wed Sep 07, 2005 10:48 am
Plonked into the regex forum
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Wed Sep 07, 2005 10:52 am
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
Post
by ska » Wed Sep 07, 2005 11:29 am
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)
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 07, 2005 11:34 am
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
Post
by ska » Wed Sep 07, 2005 11:37 am
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...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 07, 2005 11:42 am
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
Post
by ska » Wed Sep 07, 2005 11:46 am
Thanks. I gave it a go and I get an error:
Code: Select all
Warning: preg_match_all(): No ending delimiter '#' found
Any idea?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 07, 2005 11:48 am
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 » Wed Sep 07, 2005 11:53 am
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...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 07, 2005 11:57 am
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 » Wed Sep 07, 2005 12:09 pm
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