2

I am trying to get the content of ul tag with multiple class names. The same syntax works for div tags but cant get it to work with the ul tag below. It just returns empty array.

<html>
  <div>
    <div>
      <div>
        <ul class="set-left farm margin-exclude">
          <li>
            <a href="#">Text</a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</html>

Below just returns empty Array.

$html->find('ul[class=set-left farm margin-exclude]');
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Quantal
  • 309
  • 1
  • 15
  • 1
    Check this answer: http://stackoverflow.com/questions/14264525/php-simple-html-dom-parser-select-only-divs-with-multiple-classes It seems that you've to find one class and check the result list with the second and so on – Francesco Gusmeroli Feb 09 '17 at 10:00

2 Answers2

2

This should do the trick

$html->find('ul.set-left.farm.margin-exclude');
kscherrer
  • 5,486
  • 2
  • 19
  • 59
2

I have this working code at my end:-

file.txt:-

<html>
  <div>
    <div>
      <div>
        <ul class="set-left farm margin-exclude">
          <li>
            <a href="#">Text</a>
          </li>
        </ul>
        <ul class="set-left farm margin-exclude">
          <li>
            <a href="#">Text2</a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</html>

query.php:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include_once('simple_html_dom.php'); // this is parser file
$html = file_get_html('file.txt');


$level_items = $html->find('*[class="set-left farm margin-exclude"]');

foreach ( $level_items as $item ) {
   echo $item;
}

Output on browser:- http://i.share.pho.to/92745ee0_o.png

Note:- download parser file here:- https://sourceforge.net/projects/simplehtmldom/files/

You can use different way like:-

$html->find('ul.set-left');

OR

$html->find('.set-left.farm.margin-exclude');

OR

$html->find('*[set-left.farm.margin-exclude]');
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98