Php simple_html_dom-手册中未涉及的问题

Php simple_html_dom-手册中未涉及的问题,php,simple-html-dom,Php,Simple Html Dom,您好,我正在使用simple_html_dom搜索具有确切类“hello”的所有标记实例 foreach($html->find('.hello')as $found 上面的内容并不能完全做到这一点,因为它还提供了类似“hello world”的类。从数组中计算并列出正确的元素很简单,但是被解析的源html会发生变化,因此这是不实际的 你知道如何为这门课找到一个确切的术语吗 谢谢试试这个: foreach($html->find('[class=hello]') as $found

您好,我正在使用simple_html_dom搜索具有确切类“hello”的所有标记实例

foreach($html->find('.hello')as $found
上面的内容并不能完全做到这一点,因为它还提供了类似“hello world”的类。从数组中计算并列出正确的元素很简单,但是被解析的源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.

谢谢你,我做到了。我花了一段时间才回复,因为电脑不允许我添加评论或打勾。