Skip to content

Instantly share code, notes, and snippets.

@derpixler
Last active July 9, 2017 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derpixler/bf71a5dcce3a5ab6dec7 to your computer and use it in GitHub Desktop.
Save derpixler/bf71a5dcce3a5ab6dec7 to your computer and use it in GitHub Desktop.
Mask mailadresses like s*****@************m
<?php
$adress = 'sample@examples.com';
echo get_masked_mailadress( $adress, 10 ) . "\n";
echo get_masked_mailadress( $adress, 95 ) . "\n";
echo get_masked_mailadress( $adress, 30 ) . "\n";
echo get_masked_mailadress( $adress, 5 ) . "\n";
# Return masked eMail adress
# Strip x% from the string length at both parts from a email adress
#
# @param $adress string mailadress
# @param $x int percent to strip
function get_masked_mailadress( $adress, $x = 70 ) {
$adress = explode( '@', $adress );
if( count( $adress ) == 2 ){
$_first_lenght = get_strip_len( strlen( $adress[0] ), $x );
$_second_lenght = get_strip_len( strlen( $adress[1] ), $x );
$adress[0] = substr( $adress[0], 0, 0 - $_first_lenght );
$adress[1] = substr( $adress[1], $_second_lenght );
return $adress[0] . get_mask_char( $_first_lenght ) . '@' . get_mask_char( $_second_lenght ) . $adress[1];
}
return 'Invalid mailadress';
}
# Calculate the strip lenght for a string
#
# @param $str string
# @param $percent int
function get_strip_len( $str, $percent ){
$len = ( $str-round( ($str*$percent/100), 0) );
if( $len == 0 ){
$len = $str + 1;
}
return $len;
}
# Returns masket chars
# @param $lenght int
function get_mask_char( $lenght ){
$mask_char = false;
for( $i = 1; $i <= $lenght; $i++ ){
$mask_char .= '*';
}
return $mask_char;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment