Skip to content

Instantly share code, notes, and snippets.

@kublermdk kublermdk/mailchimp_preferences.php Secret

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.