Question for php and regex gurus...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigBen
    Confirmed User
    • Nov 2004
    • 2299

    #1

    Question for php and regex gurus...

    I need to parse html for any image tags and store all of them in an array...

    So if this is the html:

    blah blah <img src="asdf.jpg" width="150"> more blah <img border="1" src="kasdf.gif">

    The array should be:

    $images[0] = asdf.jpg
    $images[1] =kasdf.gif

    Anybody know how I can do this?
  • mortenb
    Confirmed User
    • Jul 2004
    • 2203

    #2
    I'm thinking something similar to this:

    Code:
    preg_match_all("|src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)\"?'?`?|i", $string, $matches);

    Comment

    • BigBen
      Confirmed User
      • Nov 2004
      • 2299

      #3
      Originally posted by mortenb
      I'm thinking something similar to this:

      Code:
      preg_match_all("|src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)\"?'?`?|i", $string, $matches);

      That works great. Thank you!

      Comment

      • Why
        MFBA
        • Mar 2003
        • 7230

        #4
        there are things other then images that use src= so you might want to be more explicit with that regex

        Comment

        • BigBen
          Confirmed User
          • Nov 2004
          • 2299

          #5
          Originally posted by Why
          there are things other then images that use src= so you might want to be more explicit with that regex

          Good point. It matches anything after src even if it's not in an img tag. Any ideas?

          Comment

          • Why
            MFBA
            • Mar 2003
            • 7230

            #6
            Code:
            preg_match_all("|src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)+\.(gif|GIF|jpg|JPG)\"?'?`?|i", $string, $matches);
            might work. but i didnt test it. it would only check for gifs and jpegs though.
            Last edited by Why; 03-11-2006, 02:39 PM. Reason: php to code

            Comment

            • BigBen
              Confirmed User
              • Nov 2004
              • 2299

              #7
              Thanks for the help. What can I use to match just the image name from the string extracted from the first match?

              Ie. How do I match just pic.jpg out of: src="http://example.com/images/pic.jpg"

              I tried: preg_match("/\/.+?\.jpg/", $matches[1], $imagematch);
              but that matches the longest string possible (//example.com/images/pic.jpg)

              Thanks.

              Comment

              • psili
                Confirmed User
                • Apr 2003
                • 5526

                #8
                Originally posted by BigBen
                Thanks for the help. What can I use to match just the image name from the string extracted from the first match?

                Ie. How do I match just pic.jpg out of: src="http://example.com/images/pic.jpg"

                I tried: preg_match("/\/.+?\.jpg/", $matches[1], $imagematch);
                but that matches the longest string possible (//example.com/images/pic.jpg)

                Thanks.
                Maybe do a greedy match at first, just to get all the full & relative paths to the actual images grabbed out of the src tags. Then implode that $matched array into a string. Against that string, do a more refined match to just grab out the image names.
                Your post count means nothing.

                Comment

                • BigBen
                  Confirmed User
                  • Nov 2004
                  • 2299

                  #9
                  Originally posted by psili
                  Maybe do a greedy match at first, just to get all the full & relative paths to the actual images grabbed out of the src tags. Then implode that $matched array into a string. Against that string, do a more refined match to just grab out the image names.
                  I'm having trouble doing that second part. My first regex will store the full path and I need to do a match against that string for the single image.
                  Last edited by BigBen; 03-11-2006, 06:47 PM.

                  Comment

                  Working...