-
-
Save kublermdk/244d031af57d18a8bc53 to your computer and use it in GitHub Desktop.
mailchimp_obfuscated_email gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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