我有当前的代码,我试图用***来掩盖部分用户的电子邮件
$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 *
}
我的问题是,我应该使用哪些方法或功能来完成我想要做的事情?
由于
答案 0 :(得分:2)
使用php的substr方法。
Syntax : substr($string,$start,$length);
and substr($string,$start); to get the entire string after the "$start" position(staring from 0)
所以,
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
}
答案 1 :(得分:2)
这将为您解决问题。使用php native function substr()删除最后几个字符。然后我们在为隐藏元素添加一些星星后重建电子邮件地址。 另外,你的$ unmasked_user_email [0]应该是$ 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()手册:http://php.net/manual/en/function.substr.php
答案 2 :(得分:0)
您可以使用substr(php.net/substr)来剪切字符串的结尾,然后连接您想要的*。例如:
$user_email_hidden = substr($unmasked_user_email_string, 0, $unmasked_user_email_string_length-4) . '****'
答案 3 :(得分:0)
<?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;
?>