I'm trying to replace url links (starting http:// ...) to html hyperlinks in a string with PHP, I use a regex for this, but the problem is there's conflicts with the html "  ;" characters.
So I used this, it seems to work with normal links, but if I add the rule (^&[^;]+?;)
to avoid specials characters it doesn't work.
function shortlink($text) {
return preg_replace_callback(
'/(^|[^"])(((f|ht){1}tps?:\/\/)(^&[^;]+?;)[-a-zA-Z0-9@:;\[\]%_\+.~#!?&\/\/=]+)/i',
'url_link',
$text
);
}
And this is the callback function:
function url_link($match) {
return $match[1].'<a href="'.$match[2].'" target="_blank">'.(strlen($match[2])>50 ? substr($match[2], 0, 24).'<i>'.substr($match[2], 24, 1).'</i><span class="shortl">'.substr($match[2], 25, -19).'</span>'.substr($match[2], -19) : $match[2]).'</a>';
}
But this:
function shortlink($text) {
return preg_replace_callback(
'/(^|[^"])(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:;\[\]%_\+.~#!?&\/\/=]+)/i',
'url_link',
$text
);
}
Work fine, but the conflict still exists.
This example illustrates the problem. If I have a string
http://any_link"e;
this gives =>
<a href="http://any_link'">...
But I want :
<a href="http://any_link">
Any solution?