0

私は simple_html_dom と url_to_absolute を使用して、1 つのサイトからすべての画像を取得します。しかし、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?
        }

    }

}

?>
4

2 に答える 2

1

最初の画像の後で foreach ループを中断します。

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

休憩の詳細

于 2011-03-01T10:53:30.773 に答える
0

simple_html_domはN番目の要素の取得をサポートしているようです。本当に最初の画像だけが必要な場合は、次を使用してください。

$html->find('img', 0)
于 2011-03-01T10:58:51.567 に答える