Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple html dom: how get a tag without certain attribute

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.

like image 745
leticia Avatar asked May 08 '12 19:05

leticia


2 Answers

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!

like image 148
Zuul Avatar answered Oct 04 '22 02:10

Zuul


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);});
like image 44
h0tw1r3 Avatar answered Oct 04 '22 02:10

h0tw1r3