simple_html_dom - 手册中未涉及的问题

时间:2009-10-06 22:54:36

标签: php simple-html-dom

你好我正在使用simple_html_dom

使用EXACT类“hello”搜索所有标签实例
foreach($html->find('.hello')as $found

以上并不是这样做的,因为它也给了我“hello world”这样的课程。很简单,可以计算并列出数组中的正确元素,但正在解析的源html发生了变化,因此不实用。

如何找到课程的确切术语?

由于

1 个答案:

答案 0 :(得分: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.