I just though I post something that may be useful for someone.
I was looking for a function that would take in a string of text like maybe an email in plaintext format, then find strings that look like urls and turn them into clickable links.
I found one such function on the web. it was just copied from one website to the other and everyone was just posting the same code that sucked.
I made some changes and now it works properly. See if you like it
PHP Code:function makeClickable($text, $maxurl_len = 50, $addNofollow = true, $target = '_blank')
{
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;
$nofollow = (true === $addNofollow) ? 'rel="nofollow"' : '';
$aUrls = (array_unique($urls[1]));
foreach ($aUrls as $url){
if ($maxurl_len && (strlen($url) > $maxurl_len)){
$urltext = substr($url, 0, $offset1) . '...' . substr($url, -$offset2);
}else{
$urltext = $url;
}
$pattern = '~([^\w]+)'.preg_quote($url).'([^\w]+)~';
$text = preg_replace($pattern, '\\1<a href="'. $url .'" '.$nofollow.' target="'. $target .'">'. $urltext .'</a>\\2', $text);
}
}
return $text;
}









Bookmarks