Problem with preg_match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fartsniff

    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
    }


  • Ian.H [dS]

    #2
    Re: Problem with preg_match

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Whilst lounging around on Sat, 5 Jul 2003 23:44:34 -0800, "fartsniff"
    <fart@sniff.com > amazingly managed to produce the following with
    their Etch-A-Sketch:
    [color=blue]
    > 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-".[/color]


    The regex is constructed wrongly =)

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


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

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


    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




    HTH.



    Regards,

    Ian

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

    iQA/AwUBPwfjimfqtj2 51CDhEQLjwgCcCR NaQ9XGFuXZQ9aGj ygeAomsqS4An0+o
    2gx4CHDuXRlUF0J leB5eyq54
    =4c0j
    -----END PGP SIGNATURE-----

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

    Comment

    • fartsniff

      #3
      Re: Problem with preg_match

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

      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]" <ian@WINDOZEdig iserv.net> wrote in message
      news:3kofgv869n uh01ipjut75dk0g s4a5b7ed9@4ax.c om...[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Whilst lounging around on Sat, 5 Jul 2003 23:44:34 -0800, "fartsniff"
      > <fart@sniff.com > amazingly managed to produce the following with
      > their Etch-A-Sketch:
      >[color=green]
      > > 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-".[/color]
      >
      >
      > The regex is constructed wrongly =)
      >
      >[color=green]
      > >
      > > if (preg_match('`^[sb-]+.*(gif|jpg)$`' ,$image)) {[/color]
      > ^^^^^^
      >
      >
      > This allows an 's', 'b' and / or '-' char to be used once or more,
      > hence it detects the 's' and returns true.
      >
      >[color=green]
      > > blah blah blah
      > > }
      > > elseif (preg_match('`^[st-]+.*(gif|jpg)$`' ,$image)) {
      > > blah blah blah
      > > }
      > >[/color]
      >
      >
      > 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/AwUBPwfjimfqtj2 51CDhEQLjwgCcCR NaQ9XGFuXZQ9aGj ygeAomsqS4An0+o
      > 2gx4CHDuXRlUF0J leB5eyq54
      > =4c0j
      > -----END PGP SIGNATURE-----
      >
      > --
      > Ian.H [Design & Development]
      > digiServ Network - Web solutions
      > www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
      > Programming, Web design, development & hosting.[/color]


      Comment

      Working...