Automatic Email Address Protection PHP / JavaScript
It is important to protect email addresses on websites from being harvested by spambots.
"Email spambots collect e-mail addresses from the Internet in order to build mailing lists for sending unsolicited e-mail, also known as spam. Such spambots are web crawlers that can gather e-mail addresses from Web sites, newsgroups, special-interest group (SIG) postings, and chat-room conversations. Because e-mail addresses have a distinctive format, spambots are easy to write. A number of legislators in the U.S. are reported to be devising laws that would outlaw the spambot." – Wikipedia
This is my method for automatically detecting email addresses and replacing them with a javascript that will disguise them from spambots. The script works in two stages one in javascript and the other in php.
First the javascript function that outputs the mailto link:
function sb_email(user,site){ document.write('<a href=\"mailto:' + user + '@' + site + '\">'); document.write(user + '@' + site + '<\/a>'); }
The function has 2 parameters "user" and "site", which are the two parts of the email address either side of the "@". This function can be called like so:
sb_email('joe','bloggs.com');
The second part is the php functions that recognise email addresses and replace them with the javascript function to disguise them:
//function to output the js function create_js ($matches){ $parts = explode ('@', substr ($matches[0], 1)); if(substr($parts[1],-1)=='.'){$parts[1]=substr($parts[1],0,-1); $parts[2]='.';} $str = $matches[1].'<script language="JavaScript" type="text/javascript">'; $str .= 'sb_email("'.$parts[0].'", "'.$parts[1].'");'; $str .= '</script>'.$parts[2]; return $str; } $js_callback = "create_js "; //function to recognise emails function email_protect($text){ global $js_callback; $replace = '/([> ])[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i'; $output = preg_replace_callback($replace, $js_callback, $text); return $output; }
The email_protect php function can be called on any text string like so:
echo email_protect($text);
It will automatically replace any email addresses in the text with the javascript, therefore hiding them from spambots.






Works great - thank you!
[...] Version: 0.1 - License: n/a - Author: Sam Burge - Plugin Page - » Download [...]
Is there a newer version than 0.1? I upgraded to WordPress 2.6.1 and the Plugins area is telling me that there is a new version, but the new version links to:
GALLERY LIGHTBOX PLUGIN
http://wordpress.org/extend/plugins/wp-25-gallery-lightbox-plugin/
Also, just an FYI, version 0.1 now conflicts with the Subscribe2 plugin.
Very handy little plug-in, works like a charm, thanks.
I’m experiencing a little weirdness, though. I downloaded it from your site. When I install it, it works fine but I get an alert telling me there’s a new version 1.3 (the code says “Version 0.1″) and the link to get the new version points at http://wordpress.org/extend/plugins/wp-25-gallery-lightbox-plugin/ which is clearly wrong. The “Upgrade automatically” option doesn’t work either, with the error mentioning wp-25-gallery.
Since it works, the only reason I care is I don’t want the little orange flag over “Plugins” in my dashboard, but seems like something you’d want to know.
wordpress 2.5
not working for me, i made the two changes , name and email to the php file, verified the changes were saved in the php file, but it continues to send wordpress and wordpress@domain
????