我正在通過DOM對象遍歷一個頁面,並陷入了一個點。如何區分domText和domElement對象?
這裏的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>
...
現在,這裏的部分代碼..
$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');
}
正如你可以看到.. $childNodes
回報DOMNodeList
,然後我如果在任何時候遇到DOMText
,我無法跳過它。
請讓我知道任何可能的方式,我可以把一個條件區分資源類型DOMText
和DOMElement
。
http://php.net/instanceof http://php.net/domdocument.getElementsByTagName – hakre