finding the full path of an image ?

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

    finding the full path of an image ?

    I want to list all images from a url.

    here is my code snippet which finds an image from a url:

    $url = "http://asdf.com/";

    $text = @implode("", file($url));

    while (eregi("[:space:]*(src)[:space:]*=[:space:]*([^ >]+)", $text , $regs))
    {
    echo $regs[2];
    $text = substr($text, strpos($text, $regs[1]) + strlen($regs[1]));
    }

    the only problem is that I would like to have it full path but sometimes I
    get stuff like:

    "../images/test.jpg"

    how can I get all images with full path ?


  • Janwillem Borleffs

    #2
    Re: finding the full path of an image ?

    Yang wrote:[color=blue]
    > while (eregi("[:space:]*(src)[:space:]*=[:space:]*([^ >]+)", $text ,
    > $regs)) {
    > echo $regs[2];
    > $text = substr($text, strpos($text, $regs[1]) + strlen($regs[1]));
    > }
    >[/color]

    This will also catch links like:

    <script src='foo.js' />
    [color=blue]
    > the only problem is that I would like to have it full path but
    > sometimes I get stuff like:
    >
    > "../images/test.jpg"
    >
    > how can I get all images with full path ?[/color]

    What you could do, is prepend the host and the path and apply preg_replace
    in a while loop to replace "dirname/.." sections as follows:

    $url = "http://www.example.com/somedir/../images/test.jpg";

    while (strstr($url, "/../")) {
    $url = preg_replace("|/[^/]+/\.\./|", "/", $url);
    }

    However, there are more rules to consider. Per example, links like
    "//images.example. com/images/whatever.gif". In this case, you should only
    preprend "http:" to make the link valid.

    The following file gives you an idea:




    JW



    Comment

    Working...