2

我试图通过我的 DOMDocument 对象选择没有类的图像。

我有以下

 $imgParser=new DOMDocument;

@$imgParser->loadHTML($html);

  foreach($imgParser->getElementsByTagName('img') as $imgNode){
        //the code below will display images with and without class name
        echo $imgParser->saveHTML($imgNode);

        //I can't user javascript at this point...

        //I need to save the images without class into my DB...
        //save to DB codes..

      }

有没有办法做到这一点?非常感谢!

4

1 回答 1

3

您可以使用DOMElement::hasAttribute()来确定节点是否具有特定属性。

您可以将其放在循环体的顶部以跳过该节点:

if ($imgNode->hasAttribute('class')) {
    continue; // skip node if class attribute is present
}
于 2013-02-04T17:52:04.390 回答