Search code examples
phpxpathdomxpath

DOMXpath | Select the innermost divs


Im looking for a way to select the innermost div with PHP

for example:

<div>
    <div>
        <div>
            -
        </div>
    </div>
    <div>
        <div>
            <div>
                -
            </div>
        </div>
    </div>
</div>

The DIV's containing the - would be selected in the NodeList

Im using DOMDocument and DOMXpath to go threw the html, heres and example of what one of my methods so you can see the way my class is created.

public function getkeywords()
{
    foreach($this->Xpath->query('/html/head/meta[@content][@name="keywords"][1]') as $node)
    {
        $words = $node->getAttribute('content');
        if($words)
        {
            return explode(',',str_replace(array(", "," ,"),",",$words));
        }
        return false;
    }
    return false;       
}

Solution

  • Im looking for a way to select the innermost div

    That should be:

    //div[not(descendant::div)]