fork download
  1. <?php
  2.  
  3. $html = <<<DATA
  4. <body>
  5. <h1>Text 1<img title="Not this"></h1>
  6. <h2>Text 2<img title="Not this"></h2>
  7. <h3>Text 3<img title="This"></h3>
  8. </body>
  9. DATA;
  10.  
  11. $dom = new DOMDocument('1.0', 'UTF-8');
  12. $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  13.  
  14. $xpath = new DOMXPath($dom);
  15. $imgs = $xpath->query('//h3/img[@title]');
  16. $res = array();
  17. foreach($imgs as $img) {
  18. array_push($res, $img->getAttribute('title'));
  19. }
  20.  
  21. print_r($res);
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
Array
(
    [0] => This
)