Monday, 2 February 2015

Encode Email

Encode Email

This method of hiding an email address can help to prevent spammers snarfing your email address from your page. Modern email crawlers can of course read this, but will prevent the kiddies from getting in your way.

<?php
echo encodeEmail('kevin@example.org');
/**
 *
 * Return ASCII value for web use
 *
 * @param string
 *
 * @return string
 *
 */
function makeASCII($char=0){
  return 
'&#'.ord($char).';';
}
/**
 *
 * @Encode an email to ascii
 *
 * @parma string 
 *
 * @return string
 *
 */
function encodeEmail($email){

if(
filter_var($emailFILTER_VALIDATE_EMAIL) !== FALSE)
    {
    
/*** split the email into single chars ***/
    
$charArray str_split($email);
    
/*** apply a callback funcion to each array member ***/
    
$encodedArray filter_var($charArrayFILTER_CALLBACK, array('options'=>"makeASCII"));
    
/*** put the string back together ***/
    
$encodedString implode('',$encodedArray);
    return 
'<a href="mailto:'.$encodedString.'">'.$encodedString.'</a>';
    }
else
  {
  return 
false;
  } 
}
?>

0 comments:

Post a Comment