Text to links
Converts plain-links into HTML-Links
function text2links($str='') { if($str=='' or !preg_match('/(http|www\.|@)/i', $str)) { return $str; } $lines = explode("\n", $str); $new_text = ''; while (list($k,$l) = each($lines)) { // replace links: $l = preg_replace("/([ \t]|^)www\./i", "\\1http://www.", $l); $l = preg_replace("/([ \t]|^)ftp\./i", "\\1ftp://ftp.", $l); $l = preg_replace("/(http:\/\/[^ )\r\n!]+)/i", "<a href=\"\\1\">\\1</a>", $l); $l = preg_replace("/(https:\/\/[^ )\r\n!]+)/i", "<a href=\"\\1\">\\1</a>", $l); $l = preg_replace("/(ftp:\/\/[^ )\r\n!]+)/i", "<a href=\"\\1\">\\1</a>", $l); $l = preg_replace( "/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i", "<a href=\"mailto:\\1\">\\1</a>", $l); $new_text .= $l."\n"; } return $new_text; }
Snippet Details
-
AuthorJonas John
-
LicensePublic Domain
-
LanguagePHP
-
Created03/28/2006
-
Updated03/28/2006
-
Tagslinks, regular expressions, bbcode
$text = "Visit www.jonasjohn.de :-)"; print text2links($text);
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
Özgür Özer April 22, 2011 at 02:36
this example so helpful :D
khal3d March 13, 2009 at 18:09
this Example not work with FTP link
$text = "Visit ftp.google.com";
print text2links($text);
But this work fine
$text = "Visit ftp.google.com www.khald.com";
print text2links($text);
$text = "Visit ftp.google.com";
print text2links($text);
But this work fine
$text = "Visit ftp.google.com www.khald.com";
print text2links($text);