A function to efficiently and CORRECTLY hilight any non-linked url in a text or html document. This is accomplished by first lifting all linked urls and urls that shouldn't be linked - such as those in embed, img, link ... tags. Then it will link urls that should be linked. Finally, it puts previously lifted urls back where they belong.
Download
View Source (print view)
Rating : 4.7
Views : 507
function url_hilight($str)
{
// lift all links, images and image maps
$url_tags = array (
"'<a[^>]*>.*</a>'si",
"'<map[^>]*>.*</map>'si",
"'<script[^>]*>.*</script>'si",
"'<style[^>]*>.*</style>'si",
"'<applet[^>]*>'si",
"'<object[^>]*>'si",
"'<embed[^>]*>'si",
"'<link[^>]*>'si",
"'<img[^>]*>'si"
);
foreach($url_tags as $url_tag)
{
preg_match_all($url_tag, $str, $matches, PREG_SET_ORDER);
foreach($matches as $match)
{
$key = "<" . md5($match[0]) . ">";
$search[] = $key;
$replace[] = $match[0];
}
}
$str = str_replace($replace, $search, $str);
// indicate where urls end if they have these trailing special chars
$sentinals = array("'&(quot|#34);'i", // Replace html entities
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(d+);'e");
$str = preg_replace($sentinals, "<sentinal>\0<sentinal>", $str);
$vdom = "[:alnum:]"; // Valid domain chars
$vurl = $vdom."_~-"; // Valid subdomain and path chars
//$vura = "À-ßà -ÿ!#$%&*+,;=@.".$vurl; // Valid additional parameters (after '?') chars;
$vura = "À-ßà -ÿ!#$%&*+,;=@./".$vurl; // Valid additional parameters (after '?') chars;
// insert other local characters if needed
$protocol = "[[:alpha:]]{3,10}://"; // Protocol exp
$server = "([$vurl]+[.])*[$vdom]+"; // Server name exp
$path = "(([$vurl]+([.][$vurl]+)*/)|([.]{1,2}/))*"; // Document path exp (/.../)
$name = "[$vurl]+([.][$vurl]+)*"; // Document name exp
$params = "[?][$vura]*"; // Additional parameters (for GET)
$str = eregi_replace("$protocol$server(/$path($name)?)?($params)?", "<a href="\0">\0</a>", $str); // URL into links
$str = str_replace("<sentinal>", '', $str);
return str_replace($search, $replace, $str);
}
security, snnap_ss, salars77, paper_without_wood, guest, enfactuation, rent a car, download, regirstration form, php_snips, fedex, call php', userstatusinonline, php_script"_class="neww"_target="_blank"_title="im_neuen, best ide, php script" class="neww" target="_b..., forum, p t test paired, php_tools, href php function, utfraw