I am wondering if there is a simple snippet which converts links of any kind:
http://www.cnn.com to http://www.cnn.com&l
I know is 5 years late, however I needed a similar solution and the best answer I got was from the user - erwan-dupeux-maire
Answer
I write this function. It replaces all the links in a string. Links can be in the following formats :
The second argument is the target for the link ('_blank', '_top'... can be set to false). Hope it helps...
public static function makeLinks($str, $target='_blank')
{
if ($target)
{
$target = ' target="'.$target.'"';
}
else
{
$target = '';
}
// find and replace link
$str = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '$1', $str);
// add "http://" if not set
$str = preg_replace('/]*href\s*=\s*"((?!https?:\/\/)[^"]*)"[^>]*>/i', '', $str);
return $str;
}