5

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
leticia
  • 2,390
  • 5
  • 30
  • 41

2 Answers2

6

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!

Zuul
  • 16,217
  • 6
  • 61
  • 88
  • 1
    Thanks, but that operator doesn't works for PHP code using Simple HTML DOM class (see link of it in my post). – leticia May 08 '12 at 23:30
  • yes, that is what i do, but I'm not sure if there isn't another solution, thanks, +1 for you – leticia May 09 '12 at 15:07
  • 1
    I down voted because, even though you are technically correct, the question has nothing to do with jquery. – h0tw1r3 Oct 05 '12 at 19:02
  • @KalleH.Väravas Sorry about that, I've updated the answer with a solution from the documentation. – Zuul Mar 29 '14 at 20:53
  • @h0tw1r3 While you may be correct that it is not related with jQuery, I use it as such for years now, and without any problems. Their principle is to select elements with CSS selectors just like jQuery does. Anyways, I've updated the answer with a solution to the OP's problem that is also documented on the Simple HTML DOM Parser Manual. – Zuul Mar 29 '14 at 20:56
  • 1
    Worked great! Simple and effective. +1 – Sérgio Passos Jr. Dec 26 '17 at 00:44
4

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);});
h0tw1r3
  • 6,618
  • 1
  • 28
  • 34
  • I had just posted a similar question but in my code i'm getting all elements with a class which do not have a specific ID set. Would you mind showing what the return would be if I needed to check id !=some_id Thanks – Anagio Sep 06 '13 at 06:04
  • 1
    `$nodes = array_filter($html->find('.something'), function($node){return (!empty($node->id) && $node->id != 'some_id');});` – h0tw1r3 Sep 08 '13 at 20:16
  • Thanks, I saw you suggested phpquery which accepts css3 selectors. Is there any performance difference between this and simple html dom? – Anagio Sep 09 '13 at 03:40