- preg_quote: documentation ( source)
- array_keys: documentation ( source)
- preg_replace: documentation ( source)
<?php
$pre ="<a href=\"";
$post= "</a>";
$in = array('http://">http',
'ftp://">ftp',
'mailto://">mailto',
'#">HASH',
'//">2xfw slash',
'\\\\">2x Bw slash',
'file://">file'
);
$replacements = array('/((?:href|src) *= *[\'"](?!(http|ftp|mailto|file|javascript|#|[a-z]+:|'. preg_quote('\\\\',"/"). '|\/\/)))(\/?)/i'
=> "$1" . "xxxxABSOLUTE/PATH/"
);
foreach($in as $test) {
echo '<br><pre>\n';
$r = $pre . $test . $post;
echo "$r =>" . preg_replace(array_keys($replacements), $replacements, $r);
echo '</pre>';
}