在PHP中部分隐藏电子邮件地址 [英] Partially hide email address in PHP
本文介绍了在PHP中部分隐藏电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个简单的朋友/好友系统,当有人尝试搜索新朋友时,我想显示部分隐藏的电子邮件地址,以便在不透露实际详细信息的情况下提供关于用户的身份的想法
I am building a simple friend/buddy system, and when someone tries to search for new friends, I want to show partially hidden email addresses, so as to give an idea about who the user might be, without revealing the actual details.
所以我希望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;
这可行,但是我想知道是否有更简单/更短的方法来应用相同的逻辑?可能像正则表达式一样?
This works, but I was wondering if there was any easier/shorter way of applying the same logic? Like a regex maybe?
推荐答案
以下是一些快速的方法:
here's something quick:
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