如何获得DiVs水平? [英] How To get DiVs Level?

查看:47
本文介绍了如何获得DiVs水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$html ='<html>
<head>
    <title></title>
</head>
<body>
    <div class="">
        <div class="">
           <p><strong><span style="color:#FF0000"> Content1 </span></strong></p>
           <p style="text-align:center"> Content2 <img src="https://example.com/bla1.jpg"/></p>
        </div>
       
        <h2> Header </h2>
        <div class=""><p><strong> Content3 </strong></p> </div>

    </div>

    <div class=""> Content4 </div>
    <div class="">
                   <p> Content5 </p>  
                   <p> Content6 </p> 
                   <span> blah.. </span>
    </div>
</body></html>';

我需要一个这样的数组:

I need to have such an array:

这意味着每个DIV(包括P)有一个子DIV还是一个父DIV?

This means whether each DIV (including P) has a child or parent DIV ?

推荐答案

您的尝试是不错的尝试,但我宁愿获取所有 p 标签,然后在 p爬上DOM节点层次结构> div 是当前 p 节点的父级.这样,您将只收集那些以 div 作为其父节点的 p 节点,而不会收集其他节点.换句话说,就像CSS选择器 div>p .

Yours is a nice attempt but I would rather prefer to get all p tags and then climb up the DOM node hierarchy if div is a parent of the current p node. This way, you would only collect those p nodes which has div as their parent and not otherwise. In other words, it is like the CSS selector div > p.

$ps = array();
$doc = new DomDocument('1.0', 'UTF-8');
$doc->loadHTML(mb_convert_encoding($HTML, 'HTML-ENTITIES', 'UTF-8'));

foreach($doc->getElementsByTagName('p') as $p){
   $curr_node = $p->parentNode;
   while(property_exists($curr_node,'tagName')){
      if($curr_node->tagName == 'div'){
        $ps[] = $p;
        break;
      }
      $curr_node = $curr_node->parentNode;
      if($curr_node === null) break;
   }
}

print_r($ps);


更新#1:

要获取每个 div p s,您可以递归地遍历每个 div 的所有子节点并收集所有 p s并将其添加到结果中,如下所示:

To get ps per div, you can recursively walk through all child nodes per div and collect all ps and add it to result as below:

function getPs($node,&$result){
    foreach ($node->childNodes as $c_node) {
        if(property_exists($c_node, 'tagName') && $c_node->tagName == 'p'){
            $result[] = $c_node;
        }
        getPs($c_node,$result);
    }
}

$ps = [];

foreach($doc->getElementsByTagName('div') as $div){
   $child_ps = [];
   getPs($div,$child_ps);
   if(count($child_ps) > 0) $ps[] = $child_ps;
}

echo "<pre>";
print_r($ps);


更新#2:

要获取 p 节点的HTML字符串表示形式,请更改

To get the HTML string representation of the p node, change

$result[] = $c_node;

$result[] = $c_node->ownerDocument->saveXML( $c_node );

这篇关于如何获得DiVs水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆