Lucien Dubois
Lucien Dubois

Reputation: 1704

Check if DOM element attribute is not empty

I would like to check if an attribute exists and if it is not empty. I use the PHP Simple HTML DOM Parser to explore the DOM. I tried to look under the Attribute Filters tab.

As an example I got this:

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>';

In the if I don't know how to look if the og:locale attribute is not empty.

Upvotes: 0

Views: 1185

Answers (1)

hakre
hakre

Reputation: 198198

It's just a little different angle: You do that by skipping the empty ones inside the foreach with the help of continue:

$elements = $html->find('meta[property=og:locale]');
foreach ($elements as $element) 
{
    if ($element->content === '') {
        continue;
    }
    echo $element->content;
}

Upvotes: 1

Related Questions