fork download
  1. <?php
  2.  
  3. $markup = <<<EOL
  4. <html>
  5.   <head><title>The test</title></head>
  6.   <body>
  7.   <table>
  8.   <tr>
  9.   <td><a>Foo</a></td>
  10.   <td><a>Bar</a></td>
  11.   </tr>
  12.   <tr>
  13.   <td colspan="2"><a>Baz</a></td>
  14.   </tr>
  15.   </table>
  16.   <table>
  17.   <tr><td><a>Test</a></td></tr>
  18.   </table>
  19.   </body>
  20. </html>
  21. EOL;
  22.  
  23. $dom = new DOMDocument('1.0', 'UTF-8');
  24. $dom->loadHTML($markup);
  25.  
  26. $xpath = new DOMXpath($dom);
  27. $links = $xpath->query('*/table//a');
  28.  
  29. foreach ($links as $link) {
  30. echo($link->ownerDocument->saveHTML($link) . "\n");
  31. }
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
<a>Foo</a>
<a>Bar</a>
<a>Baz</a>
<a>Test</a>