Menu

#63 Match elements that don't contain a certain value

Unassigned
closed
None
2025-11-06
2019-07-01
No

Could you add an attribute selector that "Matches elements that have the specified attribute and don't contain a certain value", ie. in this case, match all paragraphs except those who contain goo?

$html = str_get_html('<p class="foo goo">Hello</p><p class="foo bar">Dear</p><p class="bar car">World</p>');
foreach($html->find("p[class!=goo]") as $p)
  printf("%s\n", $p->innertext);

Discussion

  • LogMANOriginal

    LogMANOriginal - 2019-09-21

    Good idea.

    However, != is already supported. It checks if an attribute doesn't match the specified value (exact match). In your case this doesn't work, because you need to invert ~= (space-separated list of values).

    CSS has the :not() pseudo-class for that:

    $html->find("p:not([class~=goo])")
    

    I'll see what I can do...

     
  • LogMANOriginal

    LogMANOriginal - 2019-09-21

    Master now supports the negation pseudo-class (CSS Level 3):

    <?php
    include_once 'simple_html_dom.php';
    
    $html = str_get_html('<p class="foo goo">Hello</p><p class="foo bar">Dear</p><p class="bar car">World</p>');
    foreach($html->find("p:not([class~=goo])") as $p)
        printf("%s\n", $p->innertext);
    // Dear
    // World
    

    This should do what you need.

    [de0e10]

     

    Related

    Commit: [de0e10]

  • Anonymous

    Anonymous - 2019-09-24

    Very nice! This is most useful. Feel free to close this issue and I'll reopen it if I see any problems!

     
  • LogMANOriginal

    LogMANOriginal - 2019-10-02
    • status: open --> closed
    • assigned_to: LogMANOriginal
     
  • Anonymous

    Anonymous - 2020-09-02

    How do I do find p tags whose class contains neither foo nor bar?

    $html->find("p:not([class~=foo]) # excludes foo, but includes bar
    
     
  • Anonymous

    Anonymous - 2025-11-06

    DELETE THIS FEATURE REQUEST

     

Log in to post a comment.

MongoDB Logo MongoDB