使用简单的 html dom 解析器通过每个元素的两个或多个属性查找部分
我想说,我知道很多人认为简单 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以前从未使用过 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)据我所知,目前还没有办法做到这一点。它应该由该脚本的作者或愿意继续开发该项目的其他开发人员编辑。不知道许可证是否允许。
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.
可能八年后他们想出了一个更新版本。
要使用具有多个属性的简单 HTML DOM 解析器,
probably after eight years they have came up with an updated version.
To use simple HTML DOM parser with more than one attribute,
据我所知,通过查看 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.