Skip to content

Instantly share code, notes, and snippets.

@kublermdk
Last active August 29, 2015 13:58
Show Gist options
  • Save kublermdk/244d031af57d18a8bc53 to your computer and use it in GitHub Desktop.
Save kublermdk/244d031af57d18a8bc53 to your computer and use it in GitHub Desktop.
mailchimp_obfuscated_email gist
<?php
/**
* Mailchimp Obfuscated Email
*
* When you view an email in the Mailchimp preferences center it tries to hide some of the email address to prevent people finding it and adding it to some spam list, or other nefarious reasons.
* This function should turn something like tech@sveltestudios.com into t***@s**********.com or tech@sveltestudios.com.au into t***@s**********.***.au (note the dots shown)
*
* @param string $_email e.g 'tech@sveltestudios.com'
* @return string e.g 't***@s**********.com'
*/
function mailchimp_obfuscated_email( $_email = null )
{
if ( $_email === null || !filter_var( $_email, FILTER_VALIDATE_EMAIL ) )
{
return '';
}
$output = '';
$email_arr = str_split( $_email, 1 );
$num_chars = count( $email_arr ) - 1;
$output .= $email_arr[ 0 ]; // First char is displayed.
$array_index = 1;
$at_index = array_search( '@', $email_arr );
while ( $array_index < $at_index ) { // All the chars from the 2nd to the @ symbol are ***'d out
$output .= "*";
$array_index++;
}
$output .= $email_arr[ $at_index ];
$output .= $email_arr[ ( $at_index + 1 ) ];
$array_index = $num_chars; // Start from the end.
$dot_index = null;
while ( $dot_index === null && $array_index > 0 ) { // Check from the end trying to find the tld (top level domain)
if ( $email_arr[ $array_index ] === '.' )
{
$dot_index = $array_index;
break;
}
$array_index--;
}
$array_index = $at_index + 2;
while ( $array_index <= $num_chars ) { // From 2 chars after the @ output ***s until outputting the last dot and the tld
if ( $array_index >= $dot_index || $email_arr[ $array_index ] === '.' )
{
$output .= $email_arr[ $array_index ];
} else
{
$output .= "*";
}
$array_index++;
}
return $output; // Should return something like t***@s**********.com
}
// Example usage :
// echo mailchimp_obfuscated_email( 'tech@sveltestudios.com' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment