在PHP中使用星号(*)提示或部分隐藏电子邮件地址

slwdgvem  于 2022-11-28  发布在  PHP
关注(0)|答案(7)|浏览(367)

我的邮件地址是abcedf@example.com
如何转换成这个邮件地址a****f@example.com
我尝试使用strpos并获得@,但无法获得中间值并将其更改为****

ao218c7q

ao218c7q1#

起初,我认为@上的strpos()可以得到地址的“本地”部分的长度,我可以使用substr_replace()来简单地插入星号,但是a valid email address can have multiple @ in the local part和多字节支持是任何实际项目所必需的,这意味着mb_strpos()将是strpos()的适当替代品,但是php中还没有原生的mb_substr_replace()函数,因此设计非正则表达式代码片段的卷积变得越来越没有吸引力。
如果你想看我的原始答案,你可以查看编辑历史,但我不再认可它的使用。我的原始答案还详细说明了本页上的其他答案如何无法混淆本地子字符串中有1或2个字符的电子邮件地址。如果你正在考虑使用任何其他答案,但请确保测试对a@example.comab@example.com作为初步单元测试。
我要遵循的代码片段不验证电子邮件地址;这个代码片段的强大之处在于它是多字节安全的,在所有情况下它都会添加星号,当本地部分只有一个字符时,前导字符会在@之前重复出现,这样变异后的地址就更难猜测了。哦,并且为了更简单的维护,将要添加的星号的数目被声明为变量。
代码:(Demo)(Regex Demo

$minFill = 4;
echo preg_replace_callback(
         '/^(.)(.*?)([^@]?)(?=@[^@]+$)/u',
         function ($m) use ($minFill) {
              return $m[1]
                     . str_repeat("*", max($minFill, mb_strlen($m[2], 'UTF-8')))
                     . ($m[3] ?: $m[1]);
         },
         $email
     );

输入/输出:

'a@example.com'              => 'a****a@example.com',
'ab@example.com'             => 'a****b@example.com',
'abc@example.com'            => 'a****c@example.com',
'abcd@example.com'           => 'a****d@example.com',
'abcde@example.com'          => 'a****e@example.com',
'abcdef@example.com'         => 'a****f@example.com',
'abcdefg@example.com'        => 'a*****g@example.com',
'Ф@example.com'              => 'Ф****Ф@example.com',
'ФѰ@example.com'             => 'Ф****Ѱ@example.com',
'ФѰД@example.com'            => 'Ф****Д@example.com',
'ФѰДӐӘӔӺ@example.com'        => 'Ф*****Ӻ@example.com',
'"a@tricky@one"@example.com' => '"************"@example.com',

规则解释:

/            #pattern delimiter
^            #start of string
(.)          #capture group #1 containing the first character
(.*?)        #capture group #2 containing zero or more characters (lazy, aka non-greedy)
([^@]?)      #capture group #3 containing an optional single non-@ character
(?=@[^@]+$)  #require that the next character is @ then one or more @ until the end of the string
/            #pattern delimiter
u            #unicode/multibyte pattern modifier

回拨说明:

  • $m[1]

第一个字符(捕获组#1)

  • str_repeat("*", max($minFill, mb_strlen($m[2], 'UTF-8')))

使用UTF-8编码测量撷取群组#2的多字节长度,然后使用该计算长度与宣告的$minFill之间较高的值,然后重复*字符max()呼叫传回的次数。

  • ($m[3] ?: $m[1])

@之前的最后一个字符(捕获组#3);如果$m数组中的元素为空,则使用第一个元素的值--它将始终被填充。

gwbalxhn

gwbalxhn2#

下面是简单的版本:

$em = 'abcde@gmail.com'; // 'a****e@gmail.com'
$em = 'abcdefghi@gmail.com'; // 'a*******i@gmail.com'

使用PHP字符串函数

$stars = 4; // Min Stars to use
$at = strpos($em,'@');
if($at - 2 > $stars) $stars = $at - 2;
print substr($em,0,1) . str_repeat('*',$stars) . substr($em,$at - 1);
pgx2nnw8

pgx2nnw83#

迟到的回答却可以用途:

$email = "abcdef@gmail.com";
print preg_replace_callback('/(\w)(.*?)(\w)(@.*?)$/s', function ($matches){
    return $matches[1].preg_replace("/\w/", "*", $matches[2]).$matches[3].$matches[4];
}, $email);

# a****f@gmail.com
dbf7pr2w

dbf7pr2w4#

正则表达式:^.\K[a-zA-Z\.0-9]+(?=.@)可以更改为此^[a-zA-Z]\K[a-zA-Z\.0-9]+(?=[a-zA-Z]@)
1.^.\K这将匹配电子邮件的第一个字符,\K将重置匹配
2.[a-zA-Z\.0-9]+(?=.@)这将匹配字符串,并向前查找@之后任何字符

在代码的第二个语句中,我们将创建与match中的字符数相同的*
在第三条语句中,我们使用preg_replace删除与*匹配的字符串
Try this code snippet here

<?php
$string='abcedf@gmail.com';
preg_match('/^.\K[a-zA-Z\.0-9]+(?=.@)/',$string,$matches);//here we are gathering this part bced

$replacement= implode("",array_fill(0,strlen($matches[0]),"*"));//creating no. of *'s
echo preg_replace('/^(.)'.preg_quote($matches[0])."/", '$1'.$replacement, $string);

输出:a****f@gmail.com

pkln4tw6

pkln4tw65#

在某些情况下,您希望显示部分电子邮件,以便给予用户提示他们的电子邮件ID。如果您正在寻找类似的电子邮件,您可以将电子邮件用户名的一半字符替换为某些字符。下面是您可以在项目中使用的PHP函数to hide email address**。

<?php
function hide_email($email) {
        // extract email text before @ symbol
        $em = explode("@", $email);
        $name = implode(array_slice($em, 0, count($em) - 1), '@');

        // count half characters length to hide
        $length = floor(strlen($name) / 2);

        // Replace half characters with * symbol
        return substr($name, 0, $length) . str_repeat('*', $length) . "@" . end($em);
}

echo hide_email("contactus@gmail.com");
?>

//输出:cont****@gmail.com

a6b3iqyw

a6b3iqyw6#

可以帮助别人。

<?php
echo  hide_mail( "email@example.com.br" );

function hide_mail($email) {
    $hideAfter = 1;
    $mail_part = explode("@", $email);
    $part1 = substr($mail_part[0],0,$hideAfter);
    $part2 = substr($mail_part[0],$hideAfter);
    $part2 = str_repeat("*", strlen($part2));
    $mail_part[0] = $part1.$part2;
    return implode("@", $mail_part);
}
ef1yzkbh

ef1yzkbh7#

我找到了一个更简单的解决方案。请记住,这不是验证,需要单独完成。

$email = 'some-email@gmail.com';

preg_match('/^(.)(.+)([@])(.+)$/i', $email, $matches); 

$matches[2] = preg_replace('/./', '*', $matches[2]);

echo $matches[1] . $matches[2] . $matches[3] . $matches[4];

// output should be s*********@gmail.com.

这是我用过的页面的链接。它使用起来相当简单,而且它有一些可用的功能,你可以复制和粘贴。
https://www.phpliveregex.com/

相关问题