Page 1 of 1

Convert an e-mail address to character entities to kill spam

Posted: Thu Apr 27, 2006 11:19 am
by fantomas
I'm trying to write a PHP script to automatically convert users' e-mail addresses to character entities to display on a guestbook page without attracting spambots. There's a pretty useful tool (written in Javascript) located here:

http://wbwip.com/wbw/emailencoder.html

I wrote a similar script in PHP, but there's got to be a better way! I'm hoping that there's a function that already exists for doing this, or some sort of standard, simple method that I just haven't learned of yet. Does anyone happen to know of one, or have any suggestions to improve this script?

Thanks in advance!



BTW, this is the script that I'm using to do this currently - inefficient, yes, but it does work.

Code: Select all

// encode email
        $enc = '';
        $a = strlen($email);
        for ($i = 0; $i < $a; $i++) {
          switch ($email[$i]) {
            case "A": $enc .= "A"; break;
            case "a": $enc .= "a"; break;
            case "B": $enc .= "B"; break;
            case "b": $enc .= "b"; break;
            case "C": $enc .= "C"; break;
            case "c": $enc .= "c"; break;
            case "D": $enc .= "D"; break;
            case "d": $enc .= "d"; break;
            case "E": $enc .= "E"; break;
            case "e": $enc .= "e"; break;
            case "F": $enc .= "F"; break;
            case "f": $enc .= "f"; break;
            case "G": $enc .= "G"; break;
            case "g": $enc .= "g"; break;
            case "H": $enc .= "H"; break;
            case "h": $enc .= "h"; break;
            case "I": $enc .= "I"; break;
            case "i": $enc .= "i"; break;
            case "J": $enc .= "J"; break;
            case "j": $enc .= "j"; break;
            case "K": $enc .= "K"; break;
            case "k": $enc .= "k"; break;
            case "L": $enc .= "L"; break;
            case "l": $enc .= "l"; break;
            case "M": $enc .= "M"; break;
            case "m": $enc .= "m"; break;
            case "N": $enc .= "N"; break;
            case "n": $enc .= "n"; break;
            case "O": $enc .= "O"; break;
            case "o": $enc .= "o"; break;
            case "P": $enc .= "P"; break;
            case "p": $enc .= "p"; break;
            case "Q": $enc .= "Q"; break;
            case "q": $enc .= "q"; break;
            case "R": $enc .= "R"; break;
            case "r": $enc .= "r"; break;
            case "S": $enc .= "S"; break;
            case "s": $enc .= "s"; break;
            case "T": $enc .= "T"; break;
            case "t": $enc .= "t"; break;
            case "U": $enc .= "U"; break;
            case "u": $enc .= "u"; break;
            case "V": $enc .= "V"; break;
            case "v": $enc .= "v"; break;
            case "W": $enc .= "W"; break;
            case "w": $enc .= "w"; break;
            case "X": $enc .= "X"; break;
            case "x": $enc .= "x"; break;
            case "Y": $enc .= "Y"; break;
            case "y": $enc .= "y"; break;
            case "Z": $enc .= "Z"; break;
            case "z": $enc .= "z"; break;
            case "0": $enc .= "0"; break;
            case "1": $enc .= "1"; break;
            case "2": $enc .= "2"; break;
            case "3": $enc .= "3"; break;
            case "4": $enc .= "4"; break;
            case "5": $enc .= "5"; break;
            case "6": $enc .= "6"; break;
            case "7": $enc .= "7"; break;
            case "8": $enc .= "8"; break;
            case "9": $enc .= "9"; break;
            case "&": $enc .= "&"; break;
            case " ": $enc .= " "; break;
            case "_": $enc .= "_"; break;
            case "-": $enc .= "-"; break;
            case "@": $enc .= "@"; break;
            case ".": $enc .= "."; break;
          }
        }

Posted: Thu Apr 27, 2006 11:24 am
by R4000
isnt: &#00&#000;1; the number you get returned from ord()?
so:

Code: Select all

<?php
$email = "test@test.com";
$output = "";
for($i = 0; $i < strlen($email); $i++){
 echo "&#".ord($email{$i}).";";
}
?>
§ Code tested, works fine. and my method does work §

Posted: Thu Apr 27, 2006 11:37 am
by timvw

Posted: Thu Apr 27, 2006 11:40 am
by R4000
would only convert certain chars, he wants it to convert EVERYTHING to htmlentities :)

Posted: Thu Apr 27, 2006 11:42 am
by timvw
R4000 wrote:
would only convert certain chars, he wants it to convert EVERYTHING to htmlentities :)
Even in that case that link is good (see the user comments.)

Posted: Thu Apr 27, 2006 12:06 pm
by Christopher

Code: Select all

<?php
/*
 * by Ken Butcher based on code by John Haller
 * 
 * This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License allowing you to use 
 * this code on your own site as long as you give proper attribution and include the same license.
 */
 
function obfuscate_mailto($strEmail){
	$strNewAddress = '';
	for($intCounter = 0; $intCounter < strlen($strEmail); $intCounter++){
		$strNewAddress .= "&#" . ord(substr($strEmail,$intCounter,1)) . ";";
	}
	$arrEmail = explode("@", $strNewAddress);
	$strTag = "<script language="."Javascript"." type="."text/javascript".">\n";
	$strTag .= "<!--\n";
	$strTag .= "document.write('<a href=\"mai');\n";
	$strTag .= "document.write('lto');\n";
	$strTag .= "document.write(':" . $arrEmail[0] . "');\n";
	$strTag .= "document.write('@');\n";
	$strTag .= "document.write('" . $arrEmail[1] . "\">');\n";
	$strTag .= "document.write('" . $arrEmail[0] . "');\n";
	$strTag .= "document.write('@');\n";
	$strTag .= "document.write('" . $arrEmail[1] . "<\/a>');\n";
	$strTag .= "// -->\n";
	$strTag .= "</script><noscript>" . $arrEmail[0] . " at \n";
	$strTag .= str_replace("."," dot ",$arrEmail[1]) . "</noscript>";
	return $strTag;
}

Posted: Thu Apr 27, 2006 12:39 pm
by fantomas
Hey - thank you all very much for your help!

I've got a lot to go on now - I'm reading through the user comments at http://www.php.net/htmlentities - a lot of very good information.

R4000 - thanks for the code, that's exactly what I was searching for, and it works like a charm.

You guys are great!

Posted: Thu Apr 27, 2006 2:08 pm
by R4000
fantomas wrote:Hey - thank you all very much for your help!

I've got a lot to go on now - I'm reading through the user comments at http://www.php.net/htmlentities - a lot of very good information.

R4000 - thanks for the code, that's exactly what I was searching for, and it works like a charm.

You guys are great!
Thanks man :) I'm glad to help all of you here.