simple_html_dom - 问题不包括在手动
-
20-09-2019 - |
题
您好我正在寻找与确切类“你好”的标签所有情况下使用simple_html_dom
foreach($html->find('.hello')as $found
在上面并没有完全做到这一点,因为它也给了我类,如“世界你好”。这是简单的是计算通过从阵列,但正被解析的变化所以这不是实际的源HTML列表正确的元素。
任何想法如何找到一个类的确切期限?
由于
解决方案
尝试这种情况:
foreach($html->find('[class=hello]') as $found)
如果不工作,你总是可以做到这一点不太优雅,但仍然工作方式:
foreach($html->find('.hello') as $found)
{
if ($found->class != 'hello')
continue;
//do stuff here
}
您可以在手册中找到更多了关于这种东西,上面写着标题下的如何找到HTML元素?的。属性选择是非常强大的,在这里看到:
[attribute] Matches elements that have the specified attribute.
[attribute=value] Matches elements that have the specified attribute with a certain value.
[attribute!=value] Matches elements that don't have the specified attribute with a certain value.
[attribute^=value] Matches elements that have the specified attribute and it starts with a certain value.
[attribute$=value] Matches elements that have the specified attribute and it ends with a certain value.
[attribute*=value] Matches elements that have the specified attribute and it contains a certain value.
不隶属于 StackOverflow