simple html dom: how get a tag without certain att

2019-04-08 21:00发布

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.

2条回答
狗以群分
2楼-- · 2019-04-08 21:12

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!

查看更多
叛逆
3楼-- · 2019-04-08 21:32

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);});
查看更多
登录 后发表回答