Search code examples
phphtmlsimple-html-dom

simple html dom ul class


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]');

Solution

  • 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]');