0

I have the current code where I'm trying to mask part of user's email with ***

$user_email                         = [email protected]

$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

2
  • 3
    Fault in your code: $unmasked_user_email_string = $unmasked_user_email[0]; should be $unmasked_user_email_string = $unmasked_user_email_array[0]; Commented Oct 7, 2014 at 7:16
  • Thanks, I've corrected that in my production code :) Commented Oct 7, 2014 at 7:20

4 Answers 4

2

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                         = "[email protected]"

$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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
//[email protected] => abc@xyz.***

}   

else if ($unmasked_user_email_string_length < 5) {
//do it yourself

}

else if ($unmasked_user_email_string_length < 4) {
//do it yourself

}

Comments

0

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) . '****'

1 Comment

He said last 3 not last 4
0
<?php
$user_email                         = "[email protected]";

$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;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.