Create href from URL in Text with PHP

February 25, 2014

This is essentially a filter function that takes text that may contain URLs and converts them to actual HTML hrefs using PHP. It doesn't mess with links that are already hrefs.

function link_up($text)
{
   // link up if it's not already linked
   $text = preg_replace("/(?<!<a href=\"|url=\"|\"|])((http|ftp)+(s)?:\/\/[^\[\"<>\s]+)/ie",
			'"<a href=\"".stripslashes("\\0")."\">".stripslashes("\\0")."</a>"', $text);
 
   return $text;
}

Related Posts