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($node->childNodes as $child) { walkDOM($child); } } } function changeImagePath($html) { $dom=new DOMDocument; $dom->preserveWhiteSpace=true; $dom->loadHTML($html); $root=$dom->documentElement; walkDOM($root); $dom->formatOutput=false; return $dom->saveHTML($root); } 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.3.6 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
<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 5.0.0 - 5.3.5
Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in /in/mJkmb on line 28
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/mJkmb 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/mJkmb on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/mJkmb on line 10
Process exited with code 255.