开发者

Is there a way to search and retrieve the first image from a site using php?

Is there a way to search for an image and retrieve the url for it using php for another site, sort of like using get_meta_tags, or for example something like the facebook micropublisher, when you paste a url inside, it grabs an image associated with the site, is there a way 开发者_如何学编程to do this? any help would be greatly appreciated.


If you load the page in an XML DomDocument you can use Xpath to retrieve all Image tags. This would even allow you to create a Facebook like pager, so you can chose from several images.

PHP: Dom Document


You need to parse the DOM. Please see this link http://www.php.net/manual/en/domdocument.getelementsbytagname.php

This will return all the tags using that you can work out theway to get the first one.

This method will screen grab the website and give you as a image. you need Gd library turned on for this. http://www.perkiset.org/best/php-code/screen_grab_of_browser_window_with_php.html


You can do it with a preg_match

$source = file_get_contents("http://stackoverflow.com/questions/5434932/is-there-a-way-to-search-and-retrieve-the-first-image-from-a-site-using-php");
preg_match_all('`<img.*src="(.*(?:.png|.jpg|.jpeg|.gif))"`U', $source, $matches);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜