Monday, April 6, 2015

Convert text url to html tag - PHP Snippet

Convert text url to html tag - PHP Snippet

PHP function that convert text URL's to an HTML link.

function to_link($string){ 
return preg_replace("~(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)~", '<a href="$1://$2">$1://$2</a>$3',$string);
}

$html = 'This line of text has three urls: http://phpsnips.com http://yahoo.com and http://google.com';

echo to_link($html, TRUE);
Disqus Comments