如何使用PHP链接电子邮件地址

时间:2011-07-09 18:23:52

标签: php mysql html regex email

我发现了如何在php here中链接网址,但有没有人知道如何使用电子邮件地址执行相同的概念?谢谢!

2 个答案:

答案 0 :(得分:2)

使用preg_replace是理想的选择。

gar_onn的回答是行不通的,因为如果我写了一个像:“我使用asterisk @ home。它非常整洁”,整个字符串将被链接。

这样的事情会更理想:

$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);

答案 1 :(得分:0)

自PHP 5.3.0起,ereg_replace函数已被弃用。非常不鼓励依赖此功能。,您应该使用preg_replace

这将有效: 用于电子邮件链接

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

这将是url的没有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