473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with preg_match

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
2 4230
-----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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
0
by: Moluske | last post by:
The web site was on another server i switch to a new one but this server hav now PHP Safe Mode to ON so i get error when creating thumbnail. I cant put PHP Safe mode to OFF. is ther any thing i...
0
by: awebguynow | last post by:
I ran across this code, and it kind of made me nervous: (as an email validator) if ( !preg_match("/.*\@.*\..*/", $_POST) | preg_match("/(\)/", $_POST) ) 1) from bitwise experience with "C",...
2
by: splodge | last post by:
I have a simple regular expression: {1,5}x{1,5} Which is designed to detect the width and height of an image from a string, as in: foo 100x100 bar However, when I run this with...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
7
by: aaronic | last post by:
This is in reference to my previos post but a completely different problem. Previous problem and code can be found here: http://www.thescripts.com/forum/thread563724.html When I run the...
11
by: callieandmark | last post by:
I have a very simple file upload script which creates a thumbnail of the file (jpg) upon uploading. This works fine with small images, however, if i try to upload a file over about 1mb the...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.