By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Problem with preg_match

P: n/a
hello all,
here is a preg_match routine that i am using. basically, $image is set in
some
code above, and it can be either st-1.gif or sb-1.gif (actually it randomly
picks
them from about 100 gifs).

then it processes them based off of which image type it selected, either the
st- 's or
the sb- 's.

problem is, the preg_match that i have seems to only see the 's' then
assumes that it
"matched". if i leave the code in the order you see below, any "s" gif
matches as "sb-",
if i rearrange the code so that the preg_match "st-" is first, then any "s"
gif file matches
as "st-".

if (preg_match('`^[sb-]+.*(gif|jpg)$`',$image)) {
blah blah blah
}
elseif (preg_match('`^[st-]+.*(gif|jpg)$`',$image)) {
blah blah blah
}
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Sat, 5 Jul 2003 23:44:34 -0800, "fartsniff"
<fa**@sniff.com> amazingly managed to produce the following with
their Etch-A-Sketch:
hello all,
here is a preg_match routine that i am using. basically, $image is
set in some
code above, and it can be either st-1.gif or sb-1.gif (actually it
randomly picks
them from about 100 gifs).

then it processes them based off of which image type it selected,
either the st- 's or
the sb- 's.

problem is, the preg_match that i have seems to only see the 's'
then assumes that it
"matched". if i leave the code in the order you see below, any "s"
gif matches as "sb-",
if i rearrange the code so that the preg_match "st-" is first, then
any "s" gif file matches
as "st-".

The regex is constructed wrongly =)


if (preg_match('`^[sb-]+.*(gif|jpg)$`',$image)) { ^^^^^^
This allows an 's', 'b' and / or '-' char to be used once or more,
hence it detects the 's' and returns true.

blah blah blah
}
elseif (preg_match('`^[st-]+.*(gif|jpg)$`',$image)) {
blah blah blah
}

Same as above for this regex match.

Try:
if (preg_match("#^sb-([0-9]+)\.(gif|jpg)$#", $image)) {
and likewise for your 'st-' requirement.

A program which might help you with RegEx patterns that comes highly
recommended:
The RegEx Coach
http://weitz.de/regex-coach/

HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPwfjimfqtj251CDhEQLjwgCcCRNaQ9XGFuXZQ9aGjygeAo msqS4An0+o
2gx4CHDuXRlUF0JleB5eyq54
=4c0j
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #2

P: n/a
many thanks - your tip works great, the tool was a little difficult to
use but i found another one, regexdesigner.net.

and i forgot, all but one are in this format, st-1, bm-1, etc.
the other is like this gc-ch-1 and using that tool i came up
with this:

elseif (preg_match("#^gc-([a-z]+)-([0-9]+)\.(gif|jpg)$#", $image)) {

thanks for getting me started. it would a ton easier if the "color coding"
of dreamweaver mx "separated" the regex expressions ;)

"Ian.H [dS]" <ia*@WINDOZEdigiserv.net> wrote in message
news:3k********************************@4ax.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on Sat, 5 Jul 2003 23:44:34 -0800, "fartsniff"
<fa**@sniff.com> amazingly managed to produce the following with
their Etch-A-Sketch:
hello all,
here is a preg_match routine that i am using. basically, $image is
set in some
code above, and it can be either st-1.gif or sb-1.gif (actually it
randomly picks
them from about 100 gifs).

then it processes them based off of which image type it selected,
either the st- 's or
the sb- 's.

problem is, the preg_match that i have seems to only see the 's'
then assumes that it
"matched". if i leave the code in the order you see below, any "s"
gif matches as "sb-",
if i rearrange the code so that the preg_match "st-" is first, then
any "s" gif file matches
as "st-".

The regex is constructed wrongly =)


if (preg_match('`^[sb-]+.*(gif|jpg)$`',$image)) {

^^^^^^
This allows an 's', 'b' and / or '-' char to be used once or more,
hence it detects the 's' and returns true.

blah blah blah
}
elseif (preg_match('`^[st-]+.*(gif|jpg)$`',$image)) {
blah blah blah
}

Same as above for this regex match.

Try:
if (preg_match("#^sb-([0-9]+)\.(gif|jpg)$#", $image)) {
and likewise for your 'st-' requirement.

A program which might help you with RegEx patterns that comes highly
recommended:
The RegEx Coach
http://weitz.de/regex-coach/

HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPwfjimfqtj251CDhEQLjwgCcCRNaQ9XGFuXZQ9aGjygeAo msqS4An0+o
2gx4CHDuXRlUF0JleB5eyq54
=4c0j
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.

Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.