Sar*_*ney 3 php dom domdocument
我正在通过DOM对象遍历页面并陷入困境.
这是我必须迭代的示例HTML代码..
...
<div class="some_class">
some Text Some Text
<div class="childDiv">
</div>
<div class="childDiv">
</div>
<div class="childDiv">
</div>
<div class="childDiv">
</div>
</div>
...
Run Code Online (Sandbox Code Playgroud)
现在,这是部分代码..
$dom->loadHTML("content above");
// I want only first level child of this element.
$divs = $dom->childNodes;
foreach ($divs as $div)
{
// here the problem starts - the first node encountered is DomTEXT
// so how am i supposed to skip that and move to the other node.
$childDiv = $div->getElementsByTagName('div');
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的.. $childNodes返回DOMNodeList,然后我迭代它foreach,如果在任何时候DOMText遇到我无法跳过它.
请让我知道任何可能的方法,我可以提出区分资源类型DOMText和DOMElement.
foreach($divs as $div){
if( $div->nodeType !== 1 ) { //Element nodes are of nodeType 1. Text 3. Comments 8. etc rtm
continue;
}
$childDiv = $div->getElementsByTagName('div');
}
Run Code Online (Sandbox Code Playgroud)