PHP: Convert E-Mail Addresses from a String into Clickable Links
Posted on October 6, 2008, Filled under PHP,
Bookmark it
This is a snippet that finds e-mail addresses from a string using a regexp and converts them into clickable links.
<?php
function convert_emails_to_clickable_links($text)
{
$regex = "([a-z0-9_\-\.]+)". # name
"@". # at
"([a-z0-9-]{1,64})". # domain
"\.". # period
"([a-z]{2,10})"; # domain extension
$text = eregi_replace($regex, '<a href="mailto:\\1@\\2.\\3">\\1@\\2.\\3</a>', $text);
return $text;
}
$text = 'You can contact me at: example-name@mysite.com. My alternative address is other-username@domain.com.';
echo convert_emails_to_clickable_links($text);
/*
You can contact me at: <a href="mailto:example-name@mysite.com">example-name@mysite.com</a>. My alternative address is <a href="mailto:other-username@domain.com">other-username@domain.com</a>.
?>
*/
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- October 6, 2008
- article by Gabriel C.
- 1 comment
Sponsors
Related Posts
-
PHP: How to validate e-mail addresses using regular expressionsat May 21, 2008 with 2 comments
-
How to extract username from an e-mail address stringat September 5, 2008 with 2 comments
-
How to extract domain name from an e-mail address stringat September 5, 2008
-
Create, Customize and Send Newsletters: MeeNewsat August 3, 2009 with 1 comment

One Reply to "PHP: Convert E-Mail Addresses from a String into Clickable Links"
March 21, 2009 at 1:29 AM
That code won’t pick up any e-mail addresses with capital letters. Use eregi_replace instead of ereg_replace.