fork download
  1. <?php
  2.  
  3. $data = <<<DATA
  4. <div>
  5. <a href="#">Test,</a>
  6. <a href="/">Test1,</a>
  7. <a href="#">Test2,</a>
  8. <a href="#">Test3,</a>
  9. <a href="#">Leave me alone</a>
  10. </div>
  11. DATA;
  12.  
  13. $dom = new DOMDocument();
  14. $dom->loadHTML($data);
  15.  
  16. $xpath = new DOMXPath($dom);
  17. $links = $xpath->query("//a");
  18.  
  19. foreach ($links as $link) {
  20. $href = $link->getAttribute('href');
  21. if (substr($href, -1) === '/') {
  22. $link->setAttribute('href', $href."index.html");
  23. }
  24. }
  25. echo $dom->saveHTML();
  26. ?>
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://w...content-available-to-author-only...3.org/TR/REC-html40/loose.dtd">
<html><body><div>
	<a href="#">Test,</a>
	<a href="/index.html">Test1,</a>
	<a href="#">Test2,</a>
	<a href="#">Test3,</a>
	<a href="#">Leave me alone</a>
</div></body></html>