2014-10-07 206 views
0

一定數目的字符我有哪裏我試圖掩蓋用戶的電子郵件的一部分與***PHP替換字符串

$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 * 
} 

我的問題是,我應該用什麼方法或功能的當前代碼能夠完成我想要做的事情嗎?

感謝

+3

故障:$ unmasked_user_email_string = $ unmasked_user_email [0] ;應該是$ unmasked_user_email_string = $ unmasked_user_email_array [0]; – 2014-10-07 07:16:58

+0

謝謝,我糾正了在我的生產代碼:) – user2028856 2014-10-07 07:20:46

回答

2

這將這樣的伎倆爲您服務。使用PHP本地函數substr()刪除最後幾個字符。然後我們在爲隱藏的元素添加一些星星後重建電子郵件地址。 此外,您的$ unmasked_user_email [0]應該是$ 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]; 
} 

腓SUBSTR()手冊:在代碼http://php.net/manual/en/function.substr.php

0

您可以使用SUBSTR(php.net/substr)削減字符串的結尾,然後拼接多少*你想要的。例如:

$user_email_hidden = substr($unmasked_user_email_string, 0, $unmasked_user_email_string_length-4) . '****' 
+0

他說最後3不是最後4 – 2014-10-07 07:28:00

2

使用php的substr方法。

Syntax : substr($string,$start,$length); 

and substr($string,$start); to get the entire string after the "$start" position(staring from 0) 

參見:http://in1.php.net/substr

所以,

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

} 

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

} 

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

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

?>