3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function walkDOM($node) { if($node->nodeName=="pre" || $node->nodeName=="code") { return; } elseif($node->nodeName=="img") { $node->attributes->getNamedItem("src")->nodeValue=str_replace('desktop-img','mobile-img',$node->attributes->getNamedItem("src")->nodeValue); } elseif($node->hasChildNodes()) { foreach(range(0,$node->childNodes->length-1) as $idx) { walkDOM($node->childNodes->item($idx)); } } } function changeImagePath($html) { $dom=new DOMDocument; $dom->loadHTML($html); $root=$dom->documentElement; walkDOM($root); return $dom->saveHTML(); } echo changeImagePath(<<<HTML <div class="wrapper"> <img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image"> <img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image"> <pre> <img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image"> <img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image"> </pre> <code> <img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image"> <img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image"> </code> </div> HTML );
Output for 5.0.0 - 5.6.28, hhvm-3.10.0 - 3.13.2, 7.0.0 - 7.1.0
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><div class="wrapper"> <img src="img/demo/mobile-img/sample1.jpg" width="200" height="200" alt="image"><img src="img/demo/mobile-img/sample2.jpg" width="200" height="200" alt="image"><pre> <img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image"><img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image"></pre> <code> <img src="img/demo/desktop-img/sample1.jpg" width="200" height="200" alt="image"><img src="img/demo/desktop-img/sample2.jpg" width="200" height="200" alt="image"></code> </div></body></html>
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/rQG0c on line 10
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /in/rQG0c on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/rQG0c on line 10
Process exited with code 255.