PHPRO.ORG

How often have you needed to create a clickable link from a URL or email address. Here is a simple yet robust function to help you on your way. For this to work a valid url with protocol must be specified. eg: www.phpro.org will not work, because no protocol is provided.


<?php
/*** example usage ***/
$string="http://www.phpro.org";
echo 
makelink$string )."\n";

$string "http://phpro.org";
echo 
makelink$string )."\n";

// this is not a valid URL
$string "www.phpro.org";
echo 
makelink$string )."\n";

/**
*
* Function to make URLs into links
*
* @param string The url string
* @return string
*
**/
function makeLink($string){

/*** make sure there is an http:// on all URLs ***/
$string preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i""$1http://$2",$string);
/*** make all URLs links ***/
$string preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$string);
/*** make all emails hot links ***/
$string preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<a href=\"mailto:$1\">$1</a>",$string);

return 
$string;
}

?>