Php 检查DOM元素属性是否不为空
我想检查属性是否存在以及是否为空。 我使用来探索DOM。我试图在“属性过滤器”选项卡下查看 我举了一个例子:Php 检查DOM元素属性是否不为空,php,dom,Php,Dom,我想检查属性是否存在以及是否为空。 我使用来探索DOM。我试图在“属性过滤器”选项卡下查看 我举了一个例子: if ( $html->find('meta[property=og:locale]') && IfNotEmptyCondition ) { foreach ($html->find('meta[property=og:locale]') as $element) { echo $element->content; } }
if ( $html->find('meta[property=og:locale]') && IfNotEmptyCondition )
{
foreach ($html->find('meta[property=og:locale]') as $element) {
echo $element->content;
}
} else {
echo 'Votre site ne propose pas la balise <em>OG:locale</em>';
}
echo '<br>';
if($html->find('meta[property=og:locale]”)和&IfNotEmptyCondition)
{
foreach($html->find('meta[property=og:locale]')作为$element){
echo$element->content;
}
}否则{
echo“Votre site ne propose pas la balise OG:locale”;
}
回声“
”;
在“如果我不知道如何查看og:locale属性是否为空”中,这只是一个稍微不同的角度:您可以在以下帮助下跳过foreach中的空属性:
我终于成功了:if($html->find('meta[property=og:locale]'){foreach($html->find('meta[property=og:locale]')作为$element){if($element->content)!==''{echo$element->content;}else{echo'Votre site-ne-ne-nove-pas la-balise og:locale';}}}else{你回答自己的问题也没关系。这甚至比将所有代码强制加入一个小注释要好:)
$elements = $html->find('meta[property=og:locale]');
foreach ($elements as $element)
{
if ($element->content === '') {
continue;
}
echo $element->content;
}