3v4l.org

run code in 300+ PHP versions simultaneously
<?php $homepage = "https://example.com/"; $html = '<img class="drt" src="100.png"><img src="../101.png"><img src="/102.png"><img src="103.png">'; $check_img = preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $html, $m); foreach ($m[1] as $img){ if (strpos($img, $homepage) == false) { $old_img = $img; if (strpos($img, '../') !== false) { $img = str_replace('../', $homepage, $old_img); } elseif ($img[0] == '/') { $img = str_replace('/', $homepage, $old_img); } else { $img = $homepage.$old_img; } $html = str_replace($old_img, $img, $html); } } echo $html;

preferences:
58.2 ms | 1535 KiB | 5 Q