fork(1) download
  1. <?php
  2. $html_str = "<html>
  3. <body>
  4. Hi, this is the first image
  5. <img src='image/example.jpg' />
  6. this is the second image
  7. <img src='http://s...content-available-to-author-only...s.com/data_images/out/14/8812836-green-light-abstract.jpg' />
  8. and this is the last image
  9. <img src='image/last.png' />
  10. </body>
  11. </html>";
  12.  
  13. $doc = new DOMDocument();
  14. $doc->loadHTML($html_str);
  15.  
  16. $xp = new DOMXPath($doc);
  17. $images = $xp->query('//img[not(starts-with(@src, "http:") or starts-with(@src, "https:") or starts-with(@src, "data:"))]');
  18. foreach ($images as $img) {
  19. $img->setAttribute('src', 'http://e...content-available-to-author-only...e.com/' . ltrim($img->getAttribute('src'), '/'));
  20. }
  21. $html = $doc->saveHTML($doc->documentElement);
  22.  
  23. echo $html;
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<html><body>
                Hi, this is the first image
                    <img src="http://e...content-available-to-author-only...e.com/image/example.jpg">
                this is the second image
                    <img src="http://s...content-available-to-author-only...s.com/data_images/out/14/8812836-green-light-abstract.jpg">
                and this is the last image
                    <img src="http://e...content-available-to-author-only...e.com/image/last.png">
</body></html>