I've found many PHP script that convert urls in text to clickable links. But most of them don't work and some make big bugs. Some of them convert links that are already clickable. Others don't work and third makes parts from the text links. I need a script that will detect only links, not the text and will not convert the already clickable links because it's going on very ugly.
I found this code which seems the best from those I've tested. But it has some bugs. This code converts clickable links. Like this:
Original:
<a href="http://www.netload.in/dateiySgPP2b14W/1409423417ExpFut.pdf.htm" target="_blank">http://www.netload.in/dateiySgPP2b14W/1409...7ExpFut.pdf.htm</a>
Converted:
http://www.netload.in/dateiySgPP2b14W/1409423417ExpFut.pdf.htm" target="_blank">http://www.netload.in/dateiySgPP2b14W/1409...7ExpFut.pdf.htm
Here is the code:
function parse_urls($text, $maxurl_len = 35, $target = '_self') // Make URLs Clickable
{
if (preg_match_all('/((ht|f)tps?:\/\/([\w\.]+\.)?[\w-]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\)"\'<>\,\!]+)/si', $text, $urls))
{
$offset1 = ceil(0.65 * $maxurl_len) - 2;
$offset2 = ceil(0.30 * $maxurl_len) - 1;
foreach (array_unique($urls[1]) AS $url)
{
if ($maxurl_len AND strlen($url) > $maxurl_len)
{
$urltext = substr($url, 0, $offset1) . '...' . substr($url, -$offset2);
}
else
{
$urltext = $url;
}
$text = str_replace($url, '<a href="'. $url .'" target="'. $target .'" title="'. $url .'">'. $urltext .'</a>', $text);
}
}
return $text;
}