PHP Simple HTML DOM Parser: select only DIVs with multiple classes

I searched like crazy and didn't find a solution. The problem is simple.

Let's say I have 3 DIVs:

<div class="class1"> <div class="subclass"> TEXT1 </div> </div> <div class="class2"> <div class="subclass"> TEXT2 </div> </div> <div class="class1 class2"> <div class="subclass"> TEXT3 </div> </div> 

So very simple. I just want to find TEXT3 that has BOTH class1 and class2. Using plain HTML DOM Parser, I can't get it to work.

Here is what I tried:

 foreach($html->find("[class=class1], [class=class2]") as $item) { $items[] = $item->find('.subclass', 0)->plaintext; } 

The problem is

 find("[class=class1], [class=class2]") 

it finds all of them, since the comma is like OR, if I leave the comma, it searches for a nested class2 inside class1. I'm just looking. And ...

EDIT

Thanks to 19greg96, I found out that

 div[class=class1 class2] 

works, the problem is that he is looking for exactly those two in this order. Let's say i have

 <div class="class1 class2"> <div class="subclass"> TEXT3 </div> </div> 

then it works and if i have

 <div class="class1 class2 class3"> <div class="subclass"> TEXT3 </div> </div> 

it works when I put asterix as it searches for a substring:

 div[class*=class1 class2] 

PROBLEM

I only know that there is class1 and class3, but maybe others are in random order. This still does not work. Any idea how to just search for A and B in any random order? So that

 div[class=class1 class3] 

working with this example?

+9
source share
4 answers

EDIT 2: Since this is a bug in the dom analyzer (tested on version 1.5 ), there is no easy way to do this. The solution I could come up with:

 $find = $html->find(".class1"); $ret = array(); foreach ($find as $element) { if (strpos($element->class, 'class3') !== false) { $ret[] = $element; } } $find = $ret; 

basically you find all elements with class one, then iterate over those elements that have class two (in this case, three).


Previous answer:

Simple answer (should work according to HTML specification):

 find(".class1.class2") 

this will look for any type of element (div, img, a etc.) that has both class1 and class2. If you want to specify the type of item to add, add it to the beginning without . like:

 find("div.class1.class2") 

If there is a space between the two specified classes, it will match the elements with both the classes and the elements nested in the element with the first class:

 find(".class1 .class2") 

will match

 <div class="class1"> <div class="class2">this will be returned</div> </div> 

or

 <div class="class1 class2">this will be returned</div> 

edit: I tried your code and found that the solutions above do not work. A solution that works, however, is as follows:

 $html->find("div[class=class1 class2]") 
+19
source

You can also try the following:

test.html

 <h1 class="first second last"> <p>Paragraph</p> </h1> 

Decision:

 include "simple_html_dom.php"; $html = file_get_html('test.html'); $h1 = $html->find('h1'); foreach ($h1 as $h1) { $h1Class = ($h1->class); if($h1Class == 'first second last'){ $item['test'] = 'success'; }else{ $item['test'] = 'fail'; } $ar[] = $item; } echo "<pre>"; print_r($ar); 
+1
source

I thought simple HTML allows you to do:

 $html->find(".class1.class2") 

But I think not. You can switch to this library if you want this.

0
source

$ html-> find (div [class = classname1], div [class = classname2]);

or

$ html-> find (div.classname1, div.classname2);

-one
source

All Articles