2011-03-01 4 views
0

simple_html_dom을 사용하고 url_to_absolute는 한 사이트에서 모든 이미지를 가져옵니다. 하지만 getimagesize 판단 후 첫 번째 이미지 만 echo하는 법? 모두가 아니라? 감사. 제 잔상PHP getimagesize 판단 후 첫 번째 이미지 만 얻는 방법

<?php 
set_time_limit(0); 
require_once('simple_html_dom.php'); 
require_once('url_to_absolute.php'); 

$url = 'http://matthewjamestaylor.com/blog/how-to-post-forms-to-clean-rewritten-urls'; 

$html = file_get_html($url); 

foreach($html->find('img') as $element) { 
    $src= url_to_absolute($url, $element->src);//get http:// imgaes 

    if(preg_match('/\.(jpg|png|gif)/i',$src)){ 
    $arr = getimagesize($src); 

     if ($arr[0] >= 100 and $arr[1] >= 100) { //width and height over 100px 
      echo '<img src="'.$src.'" />'; // in here ,how to echo only the first image? not the all? 
     } 

    } 

} 

?> 

답변

1

브레이크 foreach 루프 :

 
if ($arr[0] >= 100 and $arr[1] >= 100) { 
    echo '<img src="'.$src.'" />'; 
    break; 
} 

More on break.

+0

foreach 루프를 중단하는 좋은 방법입니다. 감사합니다. – cj333

0

simple_html_dom은 N 번째 요소를 가져 오는 것을 지원합니다. 실제로 첫 번째 이미지 만 필요한 경우 다음을 사용하십시오.

$html->find('img', 0) 
관련 문제