0a635f656442d4a42963a91a822330b9b7613eea.svn-base 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. // example of how to use advanced selector features
  3. include('../simple_html_dom.php');
  4. // -----------------------------------------------------------------------------
  5. // descendant selector
  6. $str = <<<HTML
  7. <div>
  8. <div>
  9. <div class="foo bar">ok</div>
  10. </div>
  11. </div>
  12. HTML;
  13. $html = str_get_html($str);
  14. echo $html->find('div div div', 0)->innertext . '<br>'; // result: "ok"
  15. // -----------------------------------------------------------------------------
  16. // nested selector
  17. $str = <<<HTML
  18. <ul id="ul1">
  19. <li>item:<span>1</span></li>
  20. <li>item:<span>2</span></li>
  21. </ul>
  22. <ul id="ul2">
  23. <li>item:<span>3</span></li>
  24. <li>item:<span>4</span></li>
  25. </ul>
  26. HTML;
  27. $html = str_get_html($str);
  28. foreach($html->find('ul') as $ul) {
  29. foreach($ul->find('li') as $li)
  30. echo $li->innertext . '<br>';
  31. }
  32. // -----------------------------------------------------------------------------
  33. // parsing checkbox
  34. $str = <<<HTML
  35. <form name="form1" method="post" action="">
  36. <input type="checkbox" name="checkbox1" value="checkbox1" checked>item1<br>
  37. <input type="checkbox" name="checkbox2" value="checkbox2">item2<br>
  38. <input type="checkbox" name="checkbox3" value="checkbox3" checked>item3<br>
  39. </form>
  40. HTML;
  41. $html = str_get_html($str);
  42. foreach($html->find('input[type=checkbox]') as $checkbox) {
  43. if ($checkbox->checked)
  44. echo $checkbox->name . ' is checked<br>';
  45. else
  46. echo $checkbox->name . ' is not checked<br>';
  47. }
  48. ?>