fork download
  1. <?php
  2. $html = <<< EOF
  3. <a href="www.site.com/document.pdf"><img src="../images/image.jpg" /></a>
  4. <img src="../images/image1.jpg" />
  5. <a href="www.site.com/document.txt"><IMG src="../images/image2.jpg" /></a>
  6. <a href="www.site.com/document.doc"><img src="../images/image3.jpg" /></a>
  7. <a href="www.site.com/document1.pdf">My PDF</a>
  8. EOF;
  9. $doc = new DOMDocument();
  10. $doc->loadHTML($html); // loads your html
  11. $nodeList = $doc->getElementsByTagName('a');
  12. for($i=0; $i < $nodeList->length; $i++) {
  13. $node = $nodeList->item($i);
  14. $children = $node->childNodes;
  15. $hasImage = false;
  16. foreach ($children as $child) {
  17. if ($child->nodeName == 'img') {
  18. $hasImage = true;
  19. break;
  20. }
  21. }
  22. if (!$hasImage)
  23. continue;
  24. if ($node->hasAttributes())
  25. foreach ($node->attributes as $attr) {
  26. $name = $attr->nodeName;
  27. $value = $attr->nodeValue;
  28. if ( $attr->nodeName == 'href' && preg_match('/\.(doc|pdf)$/i', $attr->nodeValue) )
  29. echo $attr->nodeValue . " - Image is wrapped in a link to a PDF or DOC file\n";
  30. }
  31. }
  32. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
www.site.com/document.pdf - Image is wrapped in a link to a PDF or DOC file
www.site.com/document.doc - Image is wrapped in a link to a PDF or DOC file