Created
June 6, 2014 18:39
-
-
Save Morasta/4a1de2c2e5bc9d26b923 to your computer and use it in GitHub Desktop.
Convert plain text URLs in a string to clickable links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//From http://code.seebz.net/p/autolink-php/ | |
//Convert plain text URLs into clickable links | |
function autolink($str, $attributes=array()) { | |
$attrs = ''; | |
foreach ($attributes as $attribute => $value) { | |
$attrs .= " {$attribute}=\"{$value}\""; | |
} | |
$str = ' ' . $str; | |
$str = preg_replace( | |
'`([^"=\'>])((http|https|ftp)://[^\s<]+[^\s<\.)])`i', | |
'$1<a href="$2"'.$attrs.'>$2</a>', | |
$str | |
); | |
$str = substr($str, 1); | |
return $str; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment