views:

36

answers:

1

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;       
}
+4  A: 

Im looking for a way to select the innermost div

That should be:

//div[not(descendant::div)]
Alejandro
+1 for a correct answer.
Dimitre Novatchev
Hmm, that one is indeed better than mine +1 *(deleting mine)*.
Gordon
Do note that this is the only case where `.//` notation is really needed: i.e. `//div[not(.//div)]`
Alejandro