simple_html_dom - 手册中未涵盖的问题

发布于 2024-08-06 08:57:56 字数 255 浏览 14 评论 0原文

你好我正在使用 simple_html_dom 搜索具有精确类“hello”的标签的所有实例

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

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

有什么想法如何找到该课程的准确术语吗?

谢谢

hello I am searching for all instances of tags with the EXACT class "hello" using simple_html_dom

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

The above doesn't quite do this because it also gives me classes like "hello world". It is simple yes to count through and list the correct element from the array but the source html that is being parsed changes so that's not practical.

Any ideas how to find an exact term for the class?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-08-13 08:57:56

尝试一下:

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.

Try this:

foreach($html->find('[class=hello]') as $found)

If that doesn't work, you could always do this less elegant but still working approach:

foreach($html->find('.hello') as $found)
{
    if ($found->class != 'hello')
        continue;

    //do stuff here
}

You can find more out about this kind of stuff under the heading that says How to find HTML elements? in the manual. Attribute selectors are very powerful, see here:

[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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文