preg_match explanation

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

    #1

    preg_match explanation

    Hello
    Found this piece of code using preg_match to check file types during upload
    of files.
    $allowed_file_t ypes = "(jpg|jpeg|gif| bmp|png)";
    preg_match("/\." . $allowed_file_t ypes . "$/i",
    $_FILES['uploadedfile']["name"])

    I understand the basic preg_match but am confused as to how the string
    pattern part is working i.e.
    "/\." . $allowed_file_t ypes . "$/i"

    I need to understand it better as although it appears to work in other parts
    of my site, Im getting an error message on one page

    Warning: preg_match() expects parameter 2 to be string, array given in
    /home/iddsoftw/public_html/questiondbase/usersquestions. php on line 52

    Can anyone give a step by step explanation of what its doing
    Thanks
    Ian


  • Rik

    #2
    Re: preg_match explanation

    mantrid wrote:
    Hello
    Found this piece of code using preg_match to check file types during
    upload of files.
    No, after uploading, unfortunately.
    $allowed_file_t ypes = "(jpg|jpeg|gif| bmp|png)";
    preg_match("/\." . $allowed_file_t ypes . "$/i",
    $_FILES['uploadedfile']["name"])
    >
    I understand the basic preg_match but am confused as to how the string
    pattern part is working i.e.
    "/\." . $allowed_file_t ypes . "$/i"
    >
    I need to understand it better as although it appears to work in
    other parts of my site, Im getting an error message on one page
    >
    Warning: preg_match() expects parameter 2 to be string, array given in
    /home/iddsoftw/public_html/questiondbase/usersquestions. php on line 52
    >
    Can anyone give a step by step explanation of what its doing

    Your problem is not in the regex (which means: filename has to end on
    ..jpg/.jpeg etc.).
    Your problem is in the $_FILES part.
    If you upload 1 file, named uploadedfile, $_FILES['uploadedfile']['name']
    contains the string with the name of that file. If however you upload more
    files (several inputs named uploadedfile[] for instance),
    $_FILES['uploadedfile']['name'] will contain an array of names of the
    uploaded file. That array won't fit in preg_match, and you'll have to loop
    in some way through the $_FILES array.

    var_dump($_FILE S) in the bugging script, and all will be clear I think.

    Grtz,
    --
    Rik Wasmus


    Comment

    • mantrid

      #3
      Re: preg_match explanation

      Hi

      silly me, I am using a loop and I see now that I left the [$i] of the end of
      ['uploadedfile']["name"] in the preg_match. It works now, but I'd still like
      to know how the pattern variable is working in the code.

      Ian

      "Rik" <luiheidsgoeroe @hotmail.comwro te in message
      news:55755$4532 3f90$8259c69c$1 5987@news1.tude lft.nl...
      mantrid wrote:
      Hello
      Found this piece of code using preg_match to check file types during
      upload of files.
      >
      No, after uploading, unfortunately.
      >
      $allowed_file_t ypes = "(jpg|jpeg|gif| bmp|png)";
      preg_match("/\." . $allowed_file_t ypes . "$/i",
      $_FILES['uploadedfile']["name"])

      I understand the basic preg_match but am confused as to how the string
      pattern part is working i.e.
      "/\." . $allowed_file_t ypes . "$/i"

      I need to understand it better as although it appears to work in
      other parts of my site, Im getting an error message on one page

      Warning: preg_match() expects parameter 2 to be string, array given in
      /home/iddsoftw/public_html/questiondbase/usersquestions. php on line 52

      Can anyone give a step by step explanation of what its doing
      >
      >
      Your problem is not in the regex (which means: filename has to end on
      .jpg/.jpeg etc.).
      Your problem is in the $_FILES part.
      If you upload 1 file, named uploadedfile, $_FILES['uploadedfile']['name']
      contains the string with the name of that file. If however you upload more
      files (several inputs named uploadedfile[] for instance),
      $_FILES['uploadedfile']['name'] will contain an array of names of the
      uploaded file. That array won't fit in preg_match, and you'll have to loop
      in some way through the $_FILES array.
      >
      var_dump($_FILE S) in the bugging script, and all will be clear I think.
      >
      Grtz,
      --
      Rik Wasmus
      >
      >

      Comment

      • peter

        #4
        Re: preg_match explanation

        Found this piece of code using preg_match to check file types during
        upload
        of files.
        $allowed_file_t ypes = "(jpg|jpeg|gif| bmp|png)";
        preg_match("/\." . $allowed_file_t ypes . "$/i",
        $_FILES['uploadedfile']["name"])
        >
        I understand the basic preg_match but am confused as to how the string
        pattern part is working i.e.
        "/\." . $allowed_file_t ypes . "$/i"
        the regular expression equates to the following:-

        "/\.(jpg|jpeg|gif |bmp|png)$/i"

        basically it is saying that the string MUST contain a . (which has been
        escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see the
        | means OR when enclosed in brackets like it currently is. The $ states that
        the match must be at the end of the string (or in this case the file name)
        and the /i staes that the match should be case insensitive.



        Comment

        • mantrid

          #5
          Re: preg_match explanation

          thanks that explains a lot.
          if i wanted to make it case insensitive should I simply remove the /i ?
          also I thought || was 'or', is this special only to preg_match?
          also
          why the /\. and not just \.

          Ian


          "peter" <submit@flexiwe bhost.comwrote in message
          news:egv0dc$34o $1@emma.aioe.or g...
          >
          Found this piece of code using preg_match to check file types during
          upload
          of files.
          $allowed_file_t ypes = "(jpg|jpeg|gif| bmp|png)";
          preg_match("/\." . $allowed_file_t ypes . "$/i",
          $_FILES['uploadedfile']["name"])

          I understand the basic preg_match but am confused as to how the string
          pattern part is working i.e.
          "/\." . $allowed_file_t ypes . "$/i"
          >
          the regular expression equates to the following:-
          >
          "/\.(jpg|jpeg|gif |bmp|png)$/i"
          >
          basically it is saying that the string MUST contain a . (which has been
          escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see
          the
          | means OR when enclosed in brackets like it currently is. The $ states
          that
          the match must be at the end of the string (or in this case the file name)
          and the /i staes that the match should be case insensitive.
          >
          >
          >

          Comment

          • Colin Fine

            #6
            Re: preg_match explanation

            mantrid wrote:
            >
            "peter" <submit@flexiwe bhost.comwrote in message
            news:egv0dc$34o $1@emma.aioe.or g...
            >>Found this piece of code using preg_match to check file types during
            >>upload
            >>of files.
            >>$allowed_file _types = "(jpg|jpeg|gif| bmp|png)";
            >>preg_match( "/\." . $allowed_file_t ypes . "$/i",
            >>$_FILES['uploadedfile']["name"])
            >>>
            >>I understand the basic preg_match but am confused as to how the string
            >>pattern part is working i.e.
            >>"/\." . $allowed_file_t ypes . "$/i"
            >the regular expression equates to the following:-
            >>
            >"/\.(jpg|jpeg|gif |bmp|png)$/i"
            >>
            >basically it is saying that the string MUST contain a . (which has been
            >escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see
            the
            >| means OR when enclosed in brackets like it currently is. The $ states
            that
            >the match must be at the end of the string (or in this case the file name)
            >and the /i staes that the match should be case insensitive.
            >>
            >>
            >>
            >
            >
            (top posting corrected)
            thanks that explains a lot.
            if i wanted to make it case insensitive should I simply remove the /i ?
            also I thought || was 'or', is this special only to preg_match?
            also
            why the /\. and not just \.
            >
            Ian
            >
            Many languages distinguish '|' bitwise OR from '||' boolean OR. It had
            never occurred to me before that '|' in regexp had any connection with
            these operators: I guess therefore that the answer is that there is no
            need to distinguish two different kinds of OR so you might as well use a
            single character.

            The / .. / pair are the delimiters of the pattern. I think you can use
            any character that doesn't occur in the pattern (and I suspect that, as
            in Perl, you can use matched characters like '{' and '}' if they don't
            occur in the pattern, though I can't quickly find a reference to say so).

            We talk of the modifier as being '/i', but really it's just 'i'
            immediately following the closing delimiter.

            Colin

            Comment

            • peter

              #7
              Re: preg_match explanation

              thanks that explains a lot.
              if i wanted to make it case insensitive should I simply remove the /i ?
              also I thought || was 'or', is this special only to preg_match?
              also
              why the /\. and not just \.
              as colin pointed out the / are in this case signifying the start and end of
              the pattern you could have quite easily for example used & which I have done
              in the past when it seemed more appropriate. the \ is actually escaping the
              .. as it has special meaning (a . matches any single character and of course
              ... matches 2 characters).

              1 thing to watch with regular expressions is that certain characters have 1
              meaning in 1 situation and another meaning ina different situation for
              example ^ at the start of a regular expression means to match at the start
              of a string (or new line depending on the mode you are running the regular
              expression and of course which language the regular expression is used in)
              but it also means anything but in a character set for example the following
              would match anything BUT a w [^w].


              Comment

              Working...