3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $pre ="<a href=\""; $post= "</a>"; $in = array('http://">http', 'ftp://">ftp', 'mailto://">mailto', '#">HASH', '//">2xfw slash', '\\hostname\xyz\Informatiker.mp4"> bs bs', '\\\\">2x Bw slash', 'file://">file', 'relative/path">relative/path' ); $replacements = array('/((?:href|src) *= *[\'"](?!(http|ftp|mailto|file|javascript|#|[a-z]+:|xx\\\\|\/\/)))(\/?)/i' => "$1" . "http://www.server.tld/ABSOLUTE/PATH/" ); foreach($in as $test) { echo "\n". '<<pre>'; $r = $pre . $test . $post; echo "$r \t\t=>\t\t" . preg_replace(array_keys($replacements), $replacements, $r); echo '</pre>'; }
based on WYFsa
Output for 4.3.0 - 7.2.0
<<pre><a href="http://">http</a> => <a href="http://">http</a></pre> <<pre><a href="ftp://">ftp</a> => <a href="ftp://">ftp</a></pre> <<pre><a href="mailto://">mailto</a> => <a href="mailto://">mailto</a></pre> <<pre><a href="#">HASH</a> => <a href="#">HASH</a></pre> <<pre><a href="//">2xfw slash</a> => <a href="//">2xfw slash</a></pre> <<pre><a href="\hostname\xyz\Informatiker.mp4"> bs bs</a> => <a href="http://www.server.tld/ABSOLUTE/PATH/\hostname\xyz\Informatiker.mp4"> bs bs</a></pre> <<pre><a href="\\">2x Bw slash</a> => <a href="http://www.server.tld/ABSOLUTE/PATH/\\">2x Bw slash</a></pre> <<pre><a href="file://">file</a> => <a href="file://">file</a></pre> <<pre><a href="relative/path">relative/path</a> => <a href="http://www.server.tld/ABSOLUTE/PATH/relative/path">relative/path</a></pre>