Reputation: 152
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!
Upvotes: 2
Views: 341
Reputation: 154
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);
Upvotes: 2
Reputation: 4763
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
Upvotes: 0