繁体   English   中英

如何检查SimpleHTMLDom元素是否不存在

[英]How to check if a SimpleHTMLDom element does not exist

SimpleHtmldom可用于使用类description提取第一个元素的内容。

$html = str_get_html($html);
$html->find('.description', 0)

但是,如果此类不存在,PHP将抛出错误

Trying to get property of non-object

我试过了

if(!isset($html->find('.description', 0))) {
    echo 'not set';
}

if(!empty($html->find('.description', 0))) {
    echo 'not set';
}

但两者都给出了错误

Can't use method return value in write context

检查元素是否存在的正确方法是什么?

if(($html->find('.description', 0))) {
    echo 'set';
}else{
    echo 'not set';
}

http://www.php.net/manual/en/control-structures.if.php

根据SimpleHtmlDOM Api str_get_html($ html)需要一个字符串作为输入。 如果您的代码格式正确,请先使用html验证程序进行检查。

$htmlObj = str_get_html($html);
if (!is_object($htmlObj)) return; // catch errors 

// or wrap further code in 
if (is_object($htmlObj)) { /* doWork */ }
$avalable = ($element->find('span.sold-out-text', 0)) ? 1 : 0;

这对我的作品

对我来说,没有上述解决方案工作,最后我像这样检查

$html = str_get_html($html);

if($html){
    //html found
}else{
    //html not found
}

暂无
暂无

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

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