Hide Email v1.0
This is a simple little PHP script I wrote to hide e-mail addresses from computers that scan the Internet for e-mail addresses to spam. The script converts any e-mail address into an e-mail address link that is displayed in any javascript enabled browser using a javascript document.write() function.
<?
/**
* hideEmail Version 1.0 // 2005-06-22
* by Brent O'Connor (http://brent.epicserve.com)
*
* This function converts any email address into javascript
* document.write function that prints the email address as an html
* link.
* @param string $email - The email address you want to hide from
* email spiders.
* @param string $display - The string you want to display for link
* if you don't want to use the email address itself.
* @param string $subject - The subject of your email if you want
* one.
* return string
*
* Examples:
* hideEmail('username@domain.com');
* hideEmail('username@domain.com', 'Contact Me');
* hideEmail('username@domain.com', 'Contact Me',
* 'Some Stupid Subject');
*/
function hideEmail($email, $display = NULL, $subject = NULL) {
$pattern = '/^([\.a-zA-Z0-9_-]+)@([\.a-zA-Z0-9_-]+)\.'.
'([\.a-zA-Z0-9_-]+)$/';
preg_match($pattern, $email, $emailArr);
$username = $emailArr[1];
$domain = $emailArr[2];
$ext = $emailArr[3];
if (empty($display)) {
$display = "' + '".$username."' + '@' + '".
$domain."' + '.' + '".
$ext."' + '";
}
if (!empty($subject)) {
$subject = "?subject=$subject";
}
$return_value = "";
return $return_value;
}
?>
This is an example of how you can use this script to protect e-mail addresses that you are displaying from some kind of contact information database.
Example:
<?
while($row = mysql_fetch_array($result) {
<?= hideEmail($row['email'] ?>
}
?>
I plan on writing another function that works in conjunction with hideEmail that will find existing e-mail links in a large body of text and then convert them to a spam safe e-mail address.
Update:
The following is the code I came up with for using hideEmail with large text blocks that might contain multiple mailto links.
<?
/**
* This function finds all the mailto links in a large body of
* text and then sends them to the function hideEmail() which
* then replaces the mailto link with a javascript link that is
* hidden from computers scanning the net for e-mails to spam.
* @param string $text The large body of text that contains
* mailto links.
* @return string
*
* Example useage:
* <? echo hideEmailsInTextBlock($text) ?>
* <? $text = hideEmailsInTextBlock($text) ?>
*/
function hideEmailsInTextBlock($text) {
$pattern = '/]*)\shref="mailto:([^>]*)">([^<]*)<\/a>/';
$text = preg_replace_callback(
$pattern,
create_function(
'$matches',
'return hideEmail($matches[2], $matches[3], "Testing, 123");'
),
$text
);
return $text;
}
?>
Related tags: E-mail, Hide, Javascript
- Save this article for later, bookmark it!
- del.icio.us digg newsvine blinklist magnolia
Comments are closed.
Comments have been close for this post.
Latest Photos
Good Reading
- Mark Cuban's 12 Rules for Startups | Entrepreneur.com
- Welcome to Spreedly! | Spreedly
- Recurring Billing, Subscription Billing, Web 2.0 and SaaS Billing - Chargify
- Stripe
- The Best Exercise for Legs and Butt: Dumbbell Squats - YouTube
- Tips & tools for journalists who want to learn programming skills | Poynter.
Comments
Coincidentally, we just were working on a nearly identical concept here in the K-State web world to use in our people directory...