首页 > 解决方案 > 如果元素有这个或那个类,如何使用 getElementsByTagName()?

问题描述

我有很多元素<img>,我只想用“A”类或“B”类(而不是“C”类的元素)来扩展它们。

我想这样做是为了用 PHP 解析网页。

这是我的代码:

$doc = file_get_html('https://www.example.com');
foreach($doc-> getElementById('content')-> getElementsByTagName('img') as $item){   
}

我如何用.A.Bimg类推断?

foreach($doc-> getElementById('content')-> getElementsByTagName('img[class="A" OR "B"]') as $item){
}   

非常感谢您的帮助!

标签: phpparsingdomhtml-parsingsimple-html-dom

解决方案


怎么样find()

foreach($doc->getElementById('content')->find("img[class='A'][class='B']") as $item)
{
   // other code goes here 
} 

推荐阅读