繁体   English   中英

PHP simple_html_dom查找

[英]PHP simple_html_dom find

我有几个从html获取的页面。 $ html是每个页面中带有html的对象。 我现在正在从页面获取图像,有些页面只有一张图像,有些页面没有任何图像。 如果页面没有图像(例如empty ,我想在数组中放入一个值。 所以我需要类似if($html->find('img')==null)但是我找不到使用simple_html_dom的解决方案。 有任何想法或其他方式吗?

//using simple_html_dom

public function GetImages($dataArray) {
    $url = 'http://page/';

    foreach ($dataArray as $link) {
        $html = file_get_html($url . $link);
        if(is_object($html)){

            foreach ($html->find('img') as $image) {

                $images[] = $image->src;
            }

        }

    }
    return $images;
}

我在这里没有完全理解您的问题,如果您拉出一个页面,则可以使用
if( isset($html->find("img")) )检查是否设置了属性。

// If page has any images you will add their src path to images array
if( isset($html->find("img")) )
{
    for($html->find('img') as $image) {
       if(isset($image))
       {
           $images[] = $image->src;
       } else {
           $images[] = 'Empty';
       }
    }
}

如果可以重写问题if(isset($image->src))最好使用if(isset($image->src))可以更好地帮助您。

simple_html_dom开发已于YEARS AGO停止。.请使用积极开发的DOMDocument( http://php.net/manual/en/class.domdocument.php ),而不是simple_html_dom ...

if(count($html->find('img'))==0){$noImages=true;};

(在DOMDocument中,您将使用$ domd-> getElementsByTagName(“ img”),就像在javascript webdev中一样)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM