Reputation: 3203
I have the current code where I'm trying to mask part of user's email with ***
$user_email = info@test.com
$unmasked_user_email_array = explode('@', $user_email);
$unmasked_user_email_string = $unmasked_user_email[0];
$unmasked_user_email_string_length = strlen($unmasked_user_email_string);
if ($unmasked_user_email_string_length > 4) {
replace the last three characters of $unmasked_user_email_string with ***
}
else if ($unmasked_user_email_string_length < 5) {
replace the last two characters of $unmasked_user_email_string with **
}
else if ($unmasked_user_email_string_length < 4) {
replace the last one character of $unmasked_user_email_string with *
}
My question is, what methods or functions should I use to be able to accomplish what I'm trying to do?
Thanks
Upvotes: 0
Views: 88
Reputation: 3158
<?php
$user_email = "o1231@test.com";
$unmasked_user_email_array = explode("@", $user_email);
$unmasked_user_email_string = $unmasked_user_email_array[0];
$unmasked_user_email_string_length = strlen($unmasked_user_email_string);
if ($unmasked_user_email_string_length > 4) {
// replace the last three characters of $unmasked_user_email_string with ***
$unmasked_user_email_string = substr($unmasked_user_email_string, 0, -3) . '***';
}
else if ($unmasked_user_email_string_length == 5) {
// replace the last two characters of $unmasked_user_email_string with **
$unmasked_user_email_string = substr($unmasked_user_email_string, 0, -2) . '**';
}
else if (($unmasked_user_email_string_length < 4) && ($unmasked_user_email_string_length >= 2)) {
// replace the last one character of $unmasked_user_email_string with *
$unmasked_user_email_string = substr($unmasked_user_email_string, 0, -1) . '*';
}
else {
// replace the last one character of $unmasked_user_email_string with *
$unmasked_user_email_string = substr($unmasked_user_email_string, 0, -1) . '*****';
}
echo $unmasked_user_email_string;
?>
Upvotes: 0
Reputation: 3797
This will do the trick for you. Using php native function substr() to remove the last couple of characters. Then we rebuild the email address after appending some stars for hidden elements. Also, your $unmasked_user_email[0] should be $unmaskd_user_email_array[0]:
$user_email = "info@test.com"
$unmasked_user_email_array = explode('@', $user_email);
$unmasked_user_email_string = $unmasked_user_email_array[0];
$unmasked_user_email_string_length = strlen($unmasked_user_email_string);
if ($unmasked_user_email_string_length > 4) {
$email = substr( $unmasked_user_email_string, 0, -4 ) . '****' . $unmasked_user_email_array[1];
}
else if ($unmasked_user_email_string_length < 5) {
$email = substr( $unmasked_user_email_string, 0, -2 ) . '**' . $unmasked_user_email_array[1];
}
else if ($unmasked_user_email_string_length < 4) {
$email = substr( $unmasked_user_email_string, 0, -1 ) . '*' . $unmasked_user_email_array[1];
}
Php substr() manual: http://php.net/manual/en/function.substr.php
Upvotes: 2
Reputation: 3548
Use the substr method of php.
Syntax : substr($string,$start,$length);
and substr($string,$start); to get the entire string after the "$start" position(staring from 0)
See : https://www.php.net/substr
So,
if ($unmasked_user_email_string_length > 4) {
$unmasked_user_email_string with = substr($unmasked_user_email_string,0,strlen($unmasked_user_email_string)-3);
//abc@xzy.com => abc@xyz.***
}
else if ($unmasked_user_email_string_length < 5) {
//do it yourself
}
else if ($unmasked_user_email_string_length < 4) {
//do it yourself
}
Upvotes: 1
Reputation: 1790
You can use substr (php.net/substr) to cut the end of the string, and then concatenate how many * you want. For example :
$user_email_hidden = substr($unmasked_user_email_string, 0, $unmasked_user_email_string_length-4) . '****'
Upvotes: 0