By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

finding the full path of an image ?

P: n/a
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 ?
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


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

This will also catch links like:

<script src='foo.js' />
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 ?


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:

http://playground.jwscripts.com/imagegrab.phps
JW

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.