DOMXpath | Select the innermost divs

2019-06-21 12:52发布

问题:

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

回答1:

Im looking for a way to select the innermost div

That should be:

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