Simple HTML Dom: check if tag exist

acctman picture acctman · Jan 18, 2013 · Viewed 7.2k times · Source

I'm trying to check if there is and image after the tag, and if there is grab the width value. the method i'm using below is not working

$element = $html->find("td", 23);
if ($element->innertext != null) {
    $element = $html->find("td img[src=http://pictures.domain.com/images/7.gif]");
    echo $element->width . '<br />';        
} else {
   echo "empty";
}

Answer

Jasper picture Jasper · Jan 21, 2013

I got around this problem by using is_object() and is_array().

When you search for a single element, an object is returned. When you search for a set of elements, an array of objects is returned.

$td = $html->find("td", 23) is searching for a single element, so using the following will check for the existence of the searched-for element:

$td = $html->find("td", 23);
if (is_object($td)) {
    //continue
}