我正在构建一个简单的朋友/好友系统,当有人试图搜索新朋友时,我想显示部分隐藏的电子邮件地址,以便了解用户可能是谁,而不会泄露实际细节
所以我希望abcdlkjlkjk@hotmail.com
成为abcdl******@hotmail.com
。
作为测试,我写道:
<?php
$email = "abcdlkjlkjk@hotmail.com";
$em = explode("@",$email);
$name = $em[0];
$len = strlen($name);
$showLen = floor($len/2);
$str_arr = str_split($name);
for($ii=$showLen;$ii<$len;$ii++){
$str_arr[$ii] = '*';
}
$em[0] = implode('',$str_arr);
$new_name = implode('@',$em);
echo $new_name;
这有效,但我想知道是否有更简单/更短的方法来应用相同的逻辑?像正则表达式可能?
答案 0 :(得分:30)
这里有一些快速的事情:
function obfuscate_email($email)
{
$em = explode("@",$email);
$name = implode(array_slice($em, 0, count($em)-1), '@');
$len = floor(strlen($name)/2);
return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em);
}
// to see in action:
$emails = ['"Abc\@def"@iana.org', 'abcdlkjlkjk@hotmail.com'];
foreach ($emails as $email)
{
echo obfuscate_email($email) . "\n";
}
回波:
"Abc\*****@iana.org
abcdl*****@hotmail.com
答案 1 :(得分:8)
这是我的替代解决方案。
我不会使用确切数量的掩码字符来匹配电子邮件的原始长度,而是出于隐私原因使用固定长度掩码。我还会设置允许的最大字符数,以及从不显示超过一半的电子邮件。我还会屏蔽所有小于最小长度的电子邮件。
考虑到这些规则,这是我的带有可选参数的函数:
function maskEmail($email, $minLength = 3, $maxLength = 10, $mask = "***") {
$atPos = strrpos($email, "@");
$name = substr($email, 0, $atPos);
$len = strlen($name);
$domain = substr($email, $atPos);
if (($len / 2) < $maxLength) $maxLength = ($len / 2);
$shortenedEmail = (($len > $minLength) ? substr($name, 0, $maxLength) : "");
return "{$shortenedEmail}{$mask}{$domain}";
}
试验:
$email = "";
$tests = [];
for ($i=0; $i < 22; $i++) {
$email .= chr(97 + $i);
$tests[] = $email . " -> " . maskEmail("{$email}@example.com");
}
print_r($tests);
结果:
Array
(
[0] => a -> ***@example.com
[1] => ab -> ***@example.com
[2] => abc -> ***@example.com
[3] => abcd -> ab***@example.com
[4] => abcde -> ab***@example.com
[5] => abcdef -> abc***@example.com
[6] => abcdefg -> abc***@example.com
[7] => abcdefgh -> abcd***@example.com
[8] => abcdefghi -> abcd***@example.com
[9] => abcdefghij -> abcde***@example.com
[10] => abcdefghijk -> abcde***@example.com
[11] => abcdefghijkl -> abcdef***@example.com
[12] => abcdefghijklm -> abcdef***@example.com
[13] => abcdefghijklmn -> abcdefg***@example.com
[14] => abcdefghijklmno -> abcdefg***@example.com
[15] => abcdefghijklmnop -> abcdefgh***@example.com
[16] => abcdefghijklmnopq -> abcdefgh***@example.com
[17] => abcdefghijklmnopqr -> abcdefghi***@example.com
[18] => abcdefghijklmnopqrs -> abcdefghi***@example.com
[19] => abcdefghijklmnopqrst -> abcdefghij***@example.com
[20] => abcdefghijklmnopqrstu -> abcdefghij***@example.com
[21] => abcdefghijklmnopqrstuv -> abcdefghij***@example.com
)
答案 2 :(得分:4)
我创建了一个可以帮助某人的功能
function hideEmail($email)
{
$mail_parts = explode("@", $email);
$length = strlen($mail_parts[0]);
$show = floor($length/2);
$hide = $length - $show;
$replace = str_repeat("*", $hide);
return substr_replace ( $mail_parts[0] , $replace , $show, $hide ) . "@" . substr_replace($mail_parts[1], "**", 0, 2);
}
hideEmail("name@example.com"); // output: na**@**ample.com
hideEmail("something@example.com"); // output: some*****@**ample.com
您可以根据需要自定义..这样的事情(如果长度为4或更少只显示第一个)
function hideEmail($email) {
$mail_parts = explode("@", $email);
$length = strlen($mail_parts[0]);
if($length <= 4 & $length > 1)
{
$show = 1;
}else{
$show = floor($length/2);
}
$hide = $length - $show;
$replace = str_repeat("*", $hide);
return substr_replace ( $mail_parts[0] , $replace , $show, $hide ) . "@" . substr_replace($mail_parts[1], "**", 0, 2);
}
hideEmail("name@example.com"); // output: n***@**ample.com
hideEmail("something@example.com"); // output: some*****@**ample.com
答案 3 :(得分:4)
我用这个:
function secret_mail($email)
{
$prop=2;
$domain = substr(strrchr($email, "@"), 1);
$mailname=str_replace($domain,'',$email);
$name_l=strlen($mailname);
$domain_l=strlen($domain);
for($i=0;$i<=$name_l/$prop-1;$i++)
{
$start.='x';
}
for($i=0;$i<=$domain_l/$prop-1;$i++)
{
$end.='x';
}
return substr_replace($mailname, $start, 2, $name_l/$prop).substr_replace($domain, $end, 2, $domain_l/$prop);
}
输出如下内容: cyxxxxxone @ gmxxxxcom
答案 4 :(得分:3)
也许这不是你想要的,但我会这样做:
<?php
/*
Here's the logic:
We want to show X numbers.
If length of STR is less than X, hide all.
Else replace the rest with *.
*/
function mask($str, $first, $last) {
$len = strlen($str);
$toShow = $first + $last;
return substr($str, 0, $len <= $toShow ? 0 : $first).str_repeat("*", $len - ($len <= $toShow ? 0 : $toShow)).substr($str, $len - $last, $len <= $toShow ? 0 : $last);
}
function mask_email($email) {
$mail_parts = explode("@", $email);
$domain_parts = explode('.', $mail_parts[1]);
$mail_parts[0] = mask($mail_parts[0], 2, 1); // show first 2 letters and last 1 letter
$domain_parts[0] = mask($domain_parts[0], 2, 1); // same here
$mail_parts[1] = implode('.', $domain_parts);
return implode("@", $mail_parts);
}
$emails = array(
'a@a.com',
'ab@aa.com',
'abc@aaa.com',
'abcd@aaaa.com',
'abcde@aaaaa.com',
'abcdef@aaaaaa.com',
'abcdefg@aaaaaaa.com',
'abcdefgh@aaaaaaaa.com',
'abcdefghi@aaaaaaaaa.com'
);
foreach ($emails as $email){
echo '<b>'.$email.'</b><br>'.mask_email($email).'<br><hr>';
}
结果:
a@a.com
*@*.com
ab@aa.com
**@**.com
abc@aaa.com
***@***.com
abcd@aaaa.com
ab*d@aa*a.com
abcde@aaaaa.com
ab**e@aa**a.com
abcdef@aaaaaa.com
ab***f@aa***a.com
abcdefg@aaaaaaa.com
ab****g@aa****a.com
abcdefgh@aaaaaaaa.com
ab*****h@aa*****a.com
abcdefghi@aaaaaaaaa.com
ab******i@aa******a.com
答案 5 :(得分:2)
例如:
substr($email, 0, 3).'****'.substr($email, strpos($email, "@"));
这会给你类似的东西:
abc****@hotmail.com
答案 6 :(得分:1)
有时也可以显示最后一个字符。
ABCDEFZ@gmail.com成为 甲***** Z@gmail.com
我建议你保持简单。 也许这样的事情很简单 https://github.com/fedmich/PHP_Codes/blob/master/mask_email.php
使用电子邮件显示前3个字符,然后显示@符号前面的最后一个字符
function mask_email( $email ) {
/*
Author: Fed
Simple way of masking emails
*/
$char_shown = 3;
$mail_parts = explode("@", $email);
$username = $mail_parts[0];
$len = strlen( $username );
if( $len <= $char_shown ){
return implode("@", $mail_parts );
}
//Logic: show asterisk in middle, but also show the last character before @
$mail_parts[0] = substr( $username, 0 , $char_shown )
. str_repeat("*", $len - $char_shown - 1 )
. substr( $username, $len - $char_shown + 2 , 1 )
;
return implode("@", $mail_parts );
}
答案 7 :(得分:0)
试试这个功能。这适用于有效的电子邮件,例如"Abc\@def"@iana.org
。
function hideEmail($email){
$prefix = substr($email, 0, strrpos($email, '@'));
$suffix = substr($email, strripos($email, '@'));
$len = floor(strlen($prefix)/2);
return substr($prefix, 0, $len) . str_repeat('*', $len) . $suffix;
}
echo hideEmail('abcdljtrsjtrsjlkjk@hotmail.com');
echo hideEmail('"abc\@def"@iana.org');
返回
abcdljtrs*********@hotmail.com
"abc\*****@iana.org
答案 8 :(得分:0)
我有一个功能
function hide_email($email){
$final_str = '';
$string = explode('@', $email);
$leftlength = strlen($string[0]);
$string2 = explode('.', $string[1]);
$string2len = strlen($string2[0]);
$leftlength_new = $leftlength-1;
$first_letter = substr($string[0], 0,1);
$stars = '';
$stars2 = '';
for ($i=0; $i < $leftlength_new; $i++) {
$stars .= '*';
}
for ($i=0; $i < $string2len; $i++) {
$stars2 .= '*';
}
$stars;
return $final_str .= $first_letter.$stars.'@'.$stars2.'.'.$string2[1];
}
echo hide_email(&#39; Hello@PHP.com');
答案 9 :(得分:0)
如果在@之前有1个字符,则存在问题。我已修复了以下功能。
function obfuscate_email($email)
{
$em = explode("@",$email);
if(strlen($em[0])==1){
return '*'.'@'.$em[1];
}
$name = implode(array_slice($em, 0, count($em)-1), '@');
$len = floor(strlen($name)/2);
return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em);
}
答案 10 :(得分:0)
虽然这是一个老线程&amp;已经有很多答案了。我也想分享我自己的代码片段。
检查它是否是有效的电子邮件。 检查和检查多少字符?显示。 应该使用什么字符进行审查。
function get_censored_email($email, $show_chars = 3, $censor_char = '*') {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$char_length = strlen($email);
$censor_count = $char_length - $show_chars;
$return_email = substr($email, 0, $show_chars);
$return_email .= str_repeat("*", $censor_count);
return $return_email;
}
}
$email = 'noman.ibrahim115@gmail.com';
echo get_censored_email($email, 3, '*'); // returns nom***********************
答案 11 :(得分:0)
你也可以尝试这个......
<?php
$email = "abcdlkjlkjk@hotmail.com";
$resultmob = substr($email,0,5);
$resultmob .= "**********";
$resultmob .= substr($email,strpos($email, "@"));
echo $resultmob;
?>
答案: -
abcdl******@hotmail.com
答案 12 :(得分:0)
我正在使用上面的femich答案,并对我的
进行了一些调整function mask_email($email, $char_shown_front = 1, $char_shown_back = 1)
{
$mail_parts = explode('@', $email);
$username = $mail_parts[0];
$len = strlen($username);
if ($len < $char_shown_front or $len < $char_shown_back) {
return implode('@', $mail_parts);
}
//Logic: show asterisk in middle, but also show the last character before @
$mail_parts[0] = substr($username, 0, $char_shown_front)
. str_repeat('*', $len - $char_shown_front - $char_shown_back)
. substr($username, $len - $char_shown_back, $char_shown_back);
return implode('@', $mail_parts);
}
test123@gmail.com-> t ***** 3@gmail.com
您可以传入字符数以显示在正面和背面
答案 13 :(得分:0)
这里是只有两行的版本(如果您删除功能内容)。
<?php
function censor_email($str,$amount=2, $char='*') {
list($local, $domain)=explode("@",$str);
return substr($local,0,$amount).str_repeat($char,strlen($local)-$amount)."@".$domain;
}
?>
答案 14 :(得分:0)
function maskEmail($email) {
preg_match('/^.?(.*)?.@.+$/', $email, $matches);
return str_replace($matches[1], str_repeat('*', strlen($matches[1])), $email);
}
echo maskEmail('abcdefgh@example.com')
echo maskEmail('abh@example.com')
echo maskEmail('ah@example.com')
echo maskEmail('a@example.com')
返回
a******h@example.com
a*h@example.com
ah@example.com
a@example.com
答案 15 :(得分:0)
另一个已经受到广泛分享的答案影响的变体。
这有两个主要的额外好处:
示例:firstname.lastname@example.co.uk
成为f********.l*******@e*****.c*.u*
function mask_email( $email ) {
$masked = '';
$show_next = true;
foreach ( str_split( $email ) as $chr ) {
if ( $show_next ) {
$masked .= $chr;
$show_next = false;
}
else if ( in_array( $chr, array('.', '@', '+') ) ) {
$masked .= $chr;
$show_next = true;
}
else {
$masked .= '*';
$show_next = false;
}
}
return $masked;
}
答案 16 :(得分:0)
非常简单的正则表达式方式:
$email = preg_replace('/\B[^@.]/', '*', $email)
结果:
john@smith.com
:j***@s*****.c**
abcdef@example.org
:a*****@e******.o**
abcdef
:a*****