fork download
  1. <?php
  2.  
  3. $html = <<< EOF
  4. <a href="http://www.microsoft.com/"> <img src="http://c.s-microsoft.com/en-in/CMSImages/MMD_TCFamily_1006_540x304.jpg?version=ac2c5995-fde2-b40b-3f2a-b6a0baa88250" class="mscom-image feature-image" alt="Learn about Lumia 950 and Lumia 950 XL." width="540" height="304"> SOME TEXT </a>
  5. EOF;
  6.  
  7. $dom = new DOMDocument();
  8. $dom->preserveWhiteSpace = false;
  9. $dom->loadHTML($html);
  10. $xpath = new DOMXPath($dom);
  11. foreach($xpath->query("//a[contains(@href,'microsoft.com')]") as $element ){
  12. $img = $xpath->query('./img',$element)->item(0);
  13. echo $img->getAttribute('src'); // img source
  14. echo $img->getAttribute('alt'); // img alt text
  15. echo $element->textContent; //text inside the a tag
  16. }
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
http://c.s-microsoft.com/en-in/CMSImages/MMD_TCFamily_1006_540x304.jpg?version=ac2c5995-fde2-b40b-3f2a-b6a0baa88250Learn about Lumia 950 and Lumia 950 XL.    SOME TEXT