开发者

How to linkify an email address using php

I found out how to linkify a URL in php here, but does anyone know how to do this开发者_开发知识库 same concept with an email address? Thanks!


Using preg_replace would be ideal.

gar_onn's answer wouldnt work, because if i wrote a sentance like: "I use asterisk@home. It's pretty neat", the whole string would be linkified.

Something like this would be more ideal :

$pattern = '/([a-z0-9][-a-z0-9._]*[a-z0-9]*\@[a-z0-9][-a-z0-9_]+[a-z0-9]*\.[a-z0-9][-a-z0-9_-][a-z0-9]+)/i';

$str = preg_replace ($pattern, '<a href="mailto:\\1">\\1</a>', $str);


ereg_replace function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. , you should use preg_replace

this will work: for the e-mails links

 $str = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">Send email</a>',$str);  // replace a mailto (send mail)

this will for url's without erg_replace:

     $str = preg_replace('=([^\s]*)(www.)([^\s]*)=','<a href="http://\\2\\3" target=\'_new\'>\\2\\3</a>',$str); // better version to shange URL's in links
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜