Zend - The PHP Company




HTML

Add Code


URL hilight  

Type: code fragment
Added by: hdn
Entered: 14/10/2003
Last modified: 02/11/2002
Rating: - (fewer than 3 votes)
Views: 3611
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.


<?php
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$matchesPREG_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);
}
?>


Usage Example




Rate This Script





Search



This Category All Categories