使用简单的 html dom 解析器通过每个元素的两个或多个属性查找部分

发布于 2024-10-31 05:03:42 字数 471 浏览 1 评论 0原文

我想说,我知道很多人认为简单 HTML DOM 解析器对于 HTML 解析器来说是一个非常糟糕的选择。我现在仍然需要使用它。

我读过一些文章,其中描述了如何按每个元素的两个或多个属性进行搜索。 他们提出了类似的东西以及数组过滤的一种可能性

foreach ( tag[attr1=value] as tag1 )
{
   foreach ( tag[attr2=value] as tag2 )
   {
      // print tag2[attr1=value,attr2=value]
   }
}

我的问题是关于通过两个属性查找部分的本机可能性。我在手册中没有找到它,但并非所有内容都总是在手册中。

有谁知道是否有这样的方式或类似的 tag2[attr1=value,attr2=value]tag2[attr1=value attr2=value] 等?

I would like to say, that I know, that many think, that Simple HTML DOM parser is a really bad choice for HTML parser. Still I need to use it at the moment.

I read some articles where it was described how to search by two or more attributes per one element.
They proposed something like that and one possibility with array filtering

foreach ( tag[attr1=value] as tag1 )
{
   foreach ( tag[attr2=value] as tag2 )
   {
      // print tag2[attr1=value,attr2=value]
   }
}

My question is about native posibility for finding part by two attributes. I didn't find it in the manual, but not everything is always in the manual.

Does anyone know is there such way or similar tag2[attr1=value,attr2=value] or tag2[attr1=value attr2=value] or etc.?

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

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

发布评论

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

评论(5

浪菊怪哟 2024-11-07 05:03:42

以前从未使用过 Simple HTML DOM 解析器。但它的主页说它以 jQuery 方式工作,所以尝试 tag[attr1=value][attr2=value] (jQuery:多属性选择器)

Never used Simple HTML DOM parser before. But its homepage says it works in jQuery way, so try tag[attr1=value][attr2=value] (jQuery: Multiple Attribute Selector)

不即不离 2024-11-07 05:03:42
$doc      = new DOMDocument();
@$doc->loadHTML( $html );
$xpath    = new DOMXpath( $doc );
$elements = $xpath->query( "//*/div[@class='name'][@id='someId']" );  

if ( $elements->length > 0 )
{
    foreach ( $elements as $index => $node )
    {
        //get node detail here
    }
}
$doc      = new DOMDocument();
@$doc->loadHTML( $html );
$xpath    = new DOMXpath( $doc );
$elements = $xpath->query( "//*/div[@class='name'][@id='someId']" );  

if ( $elements->length > 0 )
{
    foreach ( $elements as $index => $node )
    {
        //get node detail here
    }
}
很酷又爱笑 2024-11-07 05:03:42

据我所知,目前还没有办法做到这一点。它应该由该脚本的作者或愿意继续开发该项目的其他开发人员编辑。不知道许可证是否允许。

As I see there is no way to do that at the moment. It should be edited by author of this script or by some other developer/s willing to continue the development of this project. Don't know does the license allow it or not.

何其悲哀 2024-11-07 05:03:42

可能八年后他们想出了一个更新版本。

要使用具有多个属性的简单 HTML DOM 解析器,

foreach($dom->find('tag[attr1][attr2]') as $stuff){
    echo $stuff;
}

probably after eight years they have came up with an updated version.

To use simple HTML DOM parser with more than one attribute,

foreach($dom->find('tag[attr1][attr2]') as $stuff){
    echo $stuff;
}
凉世弥音 2024-11-07 05:03:42

据我所知,通过查看 simple_html_dom,除了嵌套 foreach 循环之外,没有其他方法可以实现您正在寻找的功能。没有对 tag[attr=val][attr2=val] 的内置支持

此外,每个选择器的作用只是添加到返回的节点,而不是从它中删除,例如 tag. class[attr=val] 或 tag#id[attr=val] 我尝试将其作为一种解决方法,它会模仿一些类似的功能。

另外,我尝试了 $html->find("div[attr=val]")->find("div[attr2=val2]") 但这也失败了,因为 Simple HTML DOM返回一个节点数组而不是一个新的树对象,使得链接不可能。

最好的方法是您在问题中发布的方法。

As far as I can tell having looked through the simple_html_dom, there is no way other than a nested foreach loop to achieve the functionality you are looking for. There is no inbuilt support for tag[attr=val][attr2=val]

Additionally each selector acts simply to add to the returned nodes, never to take away from it so something like tag.class[attr=val] or tag#id[attr=val] which I tried as a work around which would have mimicked some similar functionality.

As well, I tried $html->find("div[attr=val]")->find("div[attr2=val2]") but this also failed as Simple HTML DOM returns an array of nodes rather than a new tree object making chaining impossible.

The best way to go is the way you've posted in your question.

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