Last active
November 16, 2015 16:03
-
-
Save maxim-kolesnikov/8ac2aee6547a8ead62f1 to your computer and use it in GitHub Desktop.
Делает линки из текста с урлами by Максим Колесников. Обработка юрлов. Код на PHP который преобразовывает url-адреса в html-ссылки. email-адреса становиятся mailto: адресами, чтобы открывались не как сайт, а как адрес почты.
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
class HelperUrl | |
{ | |
/** | |
* Делает линки из текста с урлами | |
* | |
* @param string текст с урлами, но без ссылок (тег <a>) | |
* | |
* @return string текст со ссылкам | |
*/ | |
public static function swapUrlOnLink($text) | |
{ | |
$text = str_replace('~~~', ' <br> ', $text); | |
$text = | |
preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", | |
"$1$2<a href=\"$3\" >$3</a>", $text); | |
$text = | |
preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", | |
"$1$2<a href=\"http://$3\" >$3</a>", $text); | |
$text = | |
preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", | |
"$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text); | |
return $text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment