Creating automatically link from string url

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 . http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos Hello this is test http://mysql.zapbuild.com/index.php?db=dev_behum&table=admins&token=34a15b853f779f699687d3594799796c my test’ is string)

Here is the source Code:

$str = “This is good boy . http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos Hello this is test http://mysql.zapbuild.com/index.php?db=dev_behum&table=admins&token=34a15b853f779f699687d3594799796c 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

OUTPUT:

This is good boy . <a target=’_blank’ href=’http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos’> http://www.youtube.com/watch?v=ehuwoGVLyhg&feature=topvideos </a> Hello this is test <a target=’_blank’ href=’http://mysql.zapbuild.com/index.php?db=dev_behum&table=admins&token=34a15b853f779f699687d3594799796c’> http://mysql.zapbuild.com/index.php?db=dev_behum&table=admins&token=34a15b853f779f699687d3594799796c </a> my test

Posted by: naresh / In: PHP
Cam