Last active
August 29, 2015 14:10
-
-
Save gekh/4b2a38b4a24283536114 to your computer and use it in GitHub Desktop.
Делает линки из текста с урлами by Максим Колесников. Обработка юрлов. Код на PHP который преобразовывает url-адреса в html-ссылки. email-адреса становиятся mailto: адресами, чтобы открывались не как сайт, а как адрес почты.
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
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
https://bitbucket.org/mkol/maxrep/src/51601bc05a40ed631bdc78d35bbee4136ddfbe41/main/url.php?at=master