<?php
function linkify($input){
$re = <<<'REGEX'
!
(
<\w++
(?:
\s++
| [^"'<>]++
| "[^"]*+"
| '[^']*+'
)*+
>
)
|
(\b https?://[^\s"'<>]++ )
|
(\b www\d*+\.\w++[^\s"'<>]++ )
!xi
REGEX;
if($m[1]) return $m[1];
return "<a rel='nofollow' href='$url'>$text</a>";
},
$input);
}
echo linkify("<img src='http://foo'>www.test.com/?x&y");
?>
PD9waHAKCmZ1bmN0aW9uIGxpbmtpZnkoJGlucHV0KXsKICAgICRyZSA9IDw8PCdSRUdFWCcKIQogICAgKAogICAgICA8XHcrKwogICAgICAoPzoKICAgICAgICBccysrCiAgICAgIHwgW14iJzw+XSsrCiAgICAgIHwgIlteIl0qKyIKICAgICAgfCAnW14nXSorJwogICAgICApKisKICAgICAgPgogICAgKQogICAgfAogICAgKFxiIGh0dHBzPzovL1teXHMiJzw+XSsrICkKICAgIHwKICAgIChcYiB3d3dcZCorXC5cdysrW15ccyInPD5dKysgKQoheGkKUkVHRVg7CiAgICAKICAgIHJldHVybiBwcmVnX3JlcGxhY2VfY2FsbGJhY2soJHJlLCBmdW5jdGlvbigkbSl7CiAgICAgICAgaWYoJG1bMV0pIHJldHVybiAkbVsxXTsKICAgICAgICAkdXJsID0gaHRtbHNwZWNpYWxjaGFycygkbVsyXSA/ICRtWzJdIDogImh0dHA6Ly8kbVszXSIpOwogICAgICAgICR0ZXh0ID0gaHRtbHNwZWNpYWxjaGFycygiJG1bMl0kbVszXSIpOwogICAgICAgIHJldHVybiAiPGEgcmVsPSdub2ZvbGxvdycgaHJlZj0nJHVybCc+JHRleHQ8L2E+IjsKICAgIH0sCiAgICAkaW5wdXQpOwp9CgplY2hvIGxpbmtpZnkoIjxpbWcgc3JjPSdodHRwOi8vZm9vJz53d3cudGVzdC5jb20vP3gmeSIpOwoKPz4=