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;
它对我有用。