How to extract images from external html page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamill
    New Member
    • Dec 2006
    • 71

    How to extract images from external html page?

    Ineed to extract images from html page using regular expression, but am not able to get desired result.
    help me.
    Bellow is my code

    [PHP]<?php
    $site='http://www.gmail.com';
    $str_text = file_get_conten ts($site);

    function str_img_src($ht ml) {
    if (strpos($html, '<img') !== false) {
    $imgsrc_regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im';
    preg_match($img src_regex, $html, $matches);
    unset($imgsrc_r egex);
    unset($html);
    if (is_array($matc hes) && !empty($matches )) {
    return $matches;
    } else {
    return false;
    }
    } else {
    return false;
    }
    }
    $ms=str_img_src ($str_text);
    foreach($ms as $m) {
    echo $m;
    }

    [/PHP]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What results are you getting?
    What aren't you getting?
    Please be more specific.

    Comment

    • kamill
      New Member
      • Dec 2006
      • 71

      #3
      If specified site contain an image within H1 tag then this code is able to return that image.
      But i need to get path of all images and also want to copy/save those images.

      Comment

      • rizwan6feb
        New Member
        • Jul 2007
        • 108

        #4
        Try this pattern
        "/<(img|IMG).*?(S RC|src)=\"(.*?) \"/"
        If you use preg_match_all, you will receive all image paths in array index 3

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by kamill
          If specified site contain an image within H1 tag then this code is able to return that image.
          But i need to get path of all images and also want to copy/save those images.
          So... what happens with the code you use?

          What output do you get?

          Comment

          • kamill
            New Member
            • Dec 2006
            • 71

            #6
            Originally posted by markusn00b
            So... what happens with the code you use?

            What output do you get?
            I am getting first image (if it exist within h1 tag) otherwise it returns false.

            Comment

            • kamill
              New Member
              • Dec 2006
              • 71

              #7
              Originally posted by rizwan6feb
              Try this pattern
              "/<(img|IMG).*?(S RC|src)=\"(.*?) \"/"
              If you use preg_match_all, you will receive all image paths in array index 3
              Dear Rizwan,
              I am still getting the same o/p.

              Comment

              Working...