Skip to content
Advertisement

Find div that has no class and no id in PHP Simple HTML DOM Parser

I am using PHP Simple HTML DOM Parser. It works well, but I have problem selecting divs that do not have both ID AND CLASS.

$html->find( 'div[!class]' )

will return all divs that has no class, but can have ID.

I tried this one but it did not work.

$html->find( 'div[!class][!id]' )

Advertisement

Answer

I don’t think that it has this sort of functionality, if you look at the code of parser

function find($selector, $idx=null, $lowercase=false)
{
    $selectors = $this->parse_selector($selector);
    if (($count=count($selectors))===0) return array();
    $found_keys = array();

    // find each selector-here it checks for each selector rather than combined one
    for ($c=0; $c<$count; ++$c)
    {

So what you can do is

$find = $html->find('div[!class]');

$selected = array();
foreach ($find as $element) {
    if (!isset($element->id)) {
        $selected[] = $element;
    }
}

$find = $selected;

So now the find will have all the elements that don’t have id and class.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement