note 13654 deleted from function.ereg-replace by jimw
| From: | jimw@php.net | Date: | Fri, 02 Nov 2001 01:58:30 +0000 |
| Subject: | note 13654 deleted from function.ereg-replace by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-20213@lists.php.net to get a copy of this message | ||
I also came up with a regular expression to link email addresses, as they work a bit differently
than normal URL's.
ereg_replace('[A-Za-z0-9]([A-Za-z0-9._]*[A-Za-z0-9]|())@[A-Za-z0-9]([A-Za-z0-9.\-]*[A-Za-z0-9]|())\.[A-Za-z]+',
'<a href="mailto:\\0">\\0</a>',
$var);
This one's a bit longer than my standard URL autolinker, as email addresses have many more
restrictions associated with them. This code will allow any email address with periods or
underscores in the account name (the part before the @), so long as they do not come first or next
to the @, and with periods or hyphens in the domain name (the part after the @), so long as they do
not come next to the @ or next to a period. Additionally, the domain name must include at least one
period. Any surrounding punctuation like a period at the end of a sentence will not be included in
the link.
Therefore, the following would be autolinked:
anon@internet.com
anon.emoose@internet-mail.com
anon_emoose@internet.mail.com
However, the following would NOT be autolinked:
anonymous
anon.@internet.com
anon@internet
anon@.com
anon@internet_mail.com
anon@internet.
.anon@internet.com
Actually, I lied. Linking on the last one would occur, but only after the initial period.
For all practical purposes, I believe this regular expression to be foolproof. Try it out, and see
for yourself.
By the way, you could also use the same regular expression to verify that a user has input a valid
email address on a form, but you would want to anchor the expression to the beginning and end of the
string.