从html检索图像


Retrieve images from html

是否有一种方法或任何东西可以搜索页面的html源并返回第n个图像的href值。该值应该这样获取:script.php?url=google.com"

// Load page as a string
$homepage = file_get_contents('http://www.example.com/');
// Create new DOM document
$doc = new DOMDocument();
// Load HTML
@$doc->loadHTML($homepage);
// Array of imagetags
$tags = $doc->getElementsByTagName('img');
// Loop trough array and echo the url
foreach ($tags as $tag)
{
    echo $tag->getAttribute('src');
}

当然。查看这些:

  • DOMDocument::loadHtmlFile
  • DOMDocument::getElementsByTagName
  • DOMNodelist::item