fork(1) download
  1. <?php
  2.  
  3. $sHtml = <<<HTML
  4. <html>
  5. <body>
  6.   <img src="../images/image.jpg" />
  7.   <a href="www.site.com/document.pdf"><img src="../images/image.jpg" /></a>
  8.   <a href="www.site.com/document.txt"><img src="../images/image.jpg" /></a>
  9.   <p>this is some text <a href="site.com/doc.pdf"> more text</p>
  10. </body>
  11. </html>
  12. HTML;
  13.  
  14. $oDoc = new DOMDocument();
  15. $oDoc->loadHTML($sHtml);
  16. $oNodeList = $oDoc->getElementsByTagName('img');
  17.  
  18. foreach($oNodeList as $t_oNode)
  19. {
  20. if($t_oNode->parentNode->nodeName === 'a')
  21. {
  22. $sLinkValue = $t_oNode->parentNode->getAttribute('href');
  23. $sExtension = substr($sLinkValue, strrpos($sLinkValue, '.'));
  24.  
  25. echo 'I am wrapped in an anchor tag '
  26. . 'and I link to a ' . $sExtension . ' file ' . "\n"
  27. ;
  28. }
  29. }
  30. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
I am wrapped in an anchor tag and I link to  a .pdf file 
I am wrapped in an anchor tag and I link to  a .txt file