1

你好,我正在使用 simple_html_dom 搜索带有 EXACT 类“hello”的所有标签实例

foreach($html->find('.hello')as $found

上面并没有完全做到这一点,因为它也给了我像“hello world”这样的类。计算并列出数组中的正确元素很简单,但是正在解析的源 html 会发生变化,因此这是不切实际的。

任何想法如何找到类的确切术语?

谢谢

4

1 回答 1

2

试试这个:

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.
于 2009-10-07T03:25:56.353 回答