Creating automatically link from string url 16Mar, 2012

Here is the function makeLinkActive to create autmoatically hyperlink( <a href=” “></a> ) in string where it will find url in a string

Parameters : $string : String to find http (In this example : ‘This is good boy . Hello this is test my test’ is string)

Here is the source Code:

$str = “This is good boy . Hello this is test my test”;

function makeLinkActive( $string=” ){
preg_match_all(‘#bhttps?://[^s()<>]+(?:([wd]+)|([^[:punct:]s]|/))#’, $string, $match);
$urls = $match[0];
$links= array();
foreach( $urls as $key => $url ) {
$links[$key] = “<a target=’_blank’ href=’$url’> $url </a>” ;
return str_replace($urls,$links,$string) ;


echo makeLinkActive( $str ); //calling function


This is good boy . <a target=’_blank’ href=’’> </a> Hello this is test <a target=’_blank’ href=’’> </a> my test

Posted by: naresh / In: PHP