Php grab first image in page
Hey guys I'm wondering how I would be able to grab only the first image on a page. I want 开发者_C百科to echo it somewhere else possibly as a variable.
well I have not attempted this before, but I found a website that looks like it gives a pretty good solution. if you are trying to copy the first image from a URL. http://phpsnips.com/snippet.php?id=61 hope that helps. :)
You use PHP's DOMDocument class to grab the page's source, use an XPath query with DOMDocument to get the first img tag, then grab its src attribute.
http://php.net/manual/en/class.domdocument.php
You can then use that to save the url, or download the file.
精彩评论