0

我需要显示谷歌通过 simple_html_dom.php 搜索的大图像

 include_once( "simple_html_dom.php" );
       $key="alexis";
       $html = new simple_html_dom();
       $html=file_get_html('http://images.google.com/images?as_q='. $key .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y');
 foreach($html->find('a') as $element) {
 preg_match('#(?:http://)?(http(s?)://([^\s]*)\.(jpg|gif|png))#', $element->href,  $imagelink);
 echo $imagelink['1'];
 }

结果:

http://beautiful-pics.org/wp-content/uploads/2011/06/Alexis_Bledel_4.jpghttp://liberalvaluesblog.com/wp-content/uploads/2009/03/alexis-bledel-gg02.jpghttp:// cdn0.sbnation.com/imported_assets/119825/alexis.jpghttp://www.hdwallpapersarena.com/wp-content/uploads/2012/04/alexis-dziena-3163.jpghttp://www.nba.com/media/ celtics/alexis-diary300422.jpghttp://beautiful-pics.org/wp-content/uploads/2011/06/Alexis_Bledel_5.jpghttp://www.popcrunch.com/wp-content/uploads/2009/03/amanda- alexis-lee2.jpghttp://www.tvfunspot.com/forums/images/realityshows/survivor16/alexis.jpghttp://access.nscpcdn.com/gallery/i/b/bledel/AlexisBlei_4705661.jpghttp://2. bp.blogspot.com/_UaLWp72nij4/S_cccrvmFsI/AAAAAAAAAMM8/yjVXCl3sHT4/s1600/alexis-bledel.jpghttp://3.bp.blogspot.com/-RFJoXjDSnmQ/T4cPwZwgrjI/AAAAAAAABlY/aNlgZ3ZFknU/s1600/Alexis%252BBledel03.jpg ://images2.fanpop。com/images/photos/4200000/Alexis-Bledel-alexis-bledel-4218179-590-768.jpghttp://www.hawktime.com/sitebuildercontent/sitebuilderpictures/AlexisArguello.jpghttp://4.bp.blogspot.com/ --iAuASq2RFc/TveqvJ2j8vI/AAAAAAAABvg/RzTmjASqK_0/s400/alexis-bledel.jpghttp://www.mtv.com/news/photos/a/american_idol/09/top36/10_alexis_grace.jpghttp://images.buddytv.com/文章/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/02/03/vanity-fair-020310-main.jpghttp://www.chorva. net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp:// www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpgcom/sitebuildercontent/sitebuilderpictures/AlexisArguello.jpghttp://4.bp.blogspot.com/--iAuASq2RFc/TveqvJ2j8vI/AAAAAAAABvg/RzTmjASqK_0/s400/alexis-bledel.jpghttp://www.mtv.com/news/photos/ a/american_idol/09/top36/10_alexis_grace.jpghttp://images.buddytv.com/articles/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/ 02/03/vanity-fair-020310-main.jpghttp://www.chorva.net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/ 3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp://www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpgcom/sitebuildercontent/sitebuilderpictures/AlexisArguello.jpghttp://4.bp.blogspot.com/--iAuASq2RFc/TveqvJ2j8vI/AAAAAAAABvg/RzTmjASqK_0/s400/alexis-bledel.jpghttp://www.mtv.com/news/photos/ a/american_idol/09/top36/10_alexis_grace.jpghttp://images.buddytv.com/articles/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/ 02/03/vanity-fair-020310-main.jpghttp://www.chorva.net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/ 3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp://www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpgcom/articles/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/02/03/vanity-fair-020310-main.jpghttp://www. chorva.net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp: //www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpgcom/articles/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/02/03/vanity-fair-020310-main.jpghttp://www. chorva.net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp: //www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpg

但我需要一个链接只用于第一张图片,谢谢

4

2 回答 2

0

添加一个break; 在以下行之后echo

...
echo $imagelink['1'];
break;
}
于 2012-05-03T22:42:04.240 回答
0
include_once( "simple_html_dom.php" );
       $key="alexis";
       $html = new simple_html_dom();
       $html=file_get_html('http://images.google.com/images?as_q='. $key .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y');
 foreach($html->find('a') as $element) {
 if(preg_match('#(?:http://)?(http(s?)://([^\s]*)\.(jpg|gif|png))#', $element->href,  $imagelink)){
    echo $imagelink[1];
    break;
 }
 }
于 2012-05-03T22:43:36.420 回答