simple html dom: how get a tag without certain attribute

余生长醉 提交于 2019-12-21 16:53:58

问题


I want to get the tags with "class" attribute equal to "someclass" but only those tags that hasn't defined the attribute "id".

I tried the following (based on this answer) but didn't work:

$html->find('.someclass[id!=*]');

Note:

I'm using Simple HTML DOM class and in the basic documentation that they give, I didn't find what I need.


回答1:


Simple HTML DOM class does not support CSS3 pseudo classes which is required for negative attribute matching.

It is simple to work around the limitation without much trouble.

$nodes = array_filter($html->find('.something'), function($node){return empty($node->id);});



回答2:


From the PHP Simple HTML DOM Parser Manual, under the How to find HTML elements?, we can read:

[!attribute] Matches elements that don't have the specified attribute.

Your code would become:

$html->find('.someclass[!id]');

This will match elements with a class someClass that do not have an id attribute.


My original answer was based on the selection of elements just like we would with jQuery since the Simple HTML DOM Parser claims to support them on their main page where we can read:

Find tags on an HTML page with selectors just like jQuery.

My sincere apologies to those who were offended by my original answer and expressed their displeasure in the comments!



来源:https://stackoverflow.com/questions/10505243/simple-html-dom-how-get-a-tag-without-certain-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!