2012-04-13 2 views
1

Как проверить, есть ли DOMDocument элементыКак проверить, есть ли DOMDocument элементы

код:

$iframe = $dom->getElementsByTagName('iframe'); 


foreach ($iframe as $if) { 

    //how to check whether $if has items for 
    // $if->item(0) so that I can access it without errors? 
} 
+0

'$ if-> item (0)' или '$ iframe-> item (0)'? См. Также ['DOMNodeList'] (http://php.net/manual/en/class.domnodelist.php). – hakre

ответ

1

тест, если узел имеет дочерние узлы с hasChildNodes()

foreach ($iframe as $if) { 
    if ($if->hasChildNodes()) { 
     // first child is $if->childNodes->item(0) 
    } 
} 
0

Вы можете проверить с помощью подобные

if($if -> hasChildNodes()) { ... }

Смежные вопросы