DOMXpath |选择最里面的div [英] DOMXpath | Select the innermost divs

查看:23
本文介绍了DOMXpath |选择最里面的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种用 PHP 选择最内层 div 的方法

例如:

<div><div>——

<div><div><div>——

将在 NodeList

中选择包含 -DIV

我使用 DOMDocument 和 DOMXpath 来抛出 html、这里和我的方法之一的示例,以便您可以看到我的类的创建方式.

公共函数getkeywords(){foreach($this->Xpath->query('/html/head/meta[@content][@name="keywords"][1]') as $node){$words = $node->getAttribute('content');如果($words){return expand(',',str_replace(array(", "," ,"),",",$words));}返回假;}返回假;}

解决方案

我正在寻找一种方法来选择最里面的 div

应该是:

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

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

解决方案

Im looking for a way to select the innermost div

That should be:

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

这篇关于DOMXpath |选择最里面的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆