3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<EOT //Find: <img src="icons/google-icon.svg" > //Replace to: <img src="{{asset('icons/google-icon.svg')}}" > //Find: <link href="css/style.css"> //Replace to: <link href="{{asset('css/style.css')}}"> /** etc... */ <a href="http://www.example.com"> EOT; $doc = new DOMDocument(); $doc->loadHTML("<div>$html</div>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($doc); foreach ($xpath->query('//img') as $img) { $src = $img->getAttribute('src'); if (preg_match('/^(?!{{\s*asset).*$/', $src, $m)) { $img->setAttribute('src', "{{asset('" . $m[0] . ")'}}"); } } foreach ($xpath->query('//link') as $link) { $href = $link->getAttribute('href'); if (preg_match('/^(?!{{\s*asset).*$/', $href, $m)) { $link->setAttribute('href', "{{asset('" . $m[0] . ")'}}"); } } // strip XML header and added <div> tag echo substr($doc->saveXML(), 44, -7);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
//Find: <img src="{{asset('icons/google-icon.svg)'}}"/> //Replace to: <img src="{{asset('icons/google-icon.svg')}}"/> //Find: <link href="{{asset('css/style.css)'}}"/> //Replace to: <link href="{{asset('css/style.css')}}"/> /** etc... */ <a href="http://www.example.com"/>

preferences:
142.46 ms | 408 KiB | 5 Q