Created
September 12, 2012 19:51
-
-
Save javiertapia/3709436 to your computer and use it in GitHub Desktop.
Activar links dentro de un texto
This file contains 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
/* | |
Busca las urls (http://..., www...) y direcciones de correo electrónico dentro de un texto | |
y los rodea con los tags <a> correspondientes. | |
*/ | |
function activar_links($text){ | |
$text = preg_replace("/(^|[\n ])([\w]*?)([\w]*?:\/\/[\w]+[^ \"\n\r\t<]*)/is", | |
"$1$2<a href=\"$3\" >$3</a>", $text); | |
$text = preg_replace("/(^|[\n ])([\w]*?)((www)\.[^ \"\t\n\r<]*)/is", | |
"$1$2<a href=\"http://$3\" >$3</a>", $text); | |
$text = preg_replace("/(^|[\n ])([\w]*?)((ftp)\.[^ \,\"\t\n\r<]*)/is", | |
"$1$2<a href=\"ftp://$3\" >$3</a>", $text); | |
$text = preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", | |
"$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text); | |
// lo siguiente corrige la posición de las comas, que es un bug del parser anterior | |
$text = str_replace(array(",</a>",",/</a>",", </a>",",/ </a>"), | |
array("</a>,","/</a>,","</a>,","/</a>,"), | |
$text); | |
$text = str_replace(array('," >'), array('">'), $text); | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment