开发者

Php grab first image in page

开发者 https://www.devze.com 2023-04-04 04:32 出处:网络
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 fou

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消