简体   繁体   English

PHP如何隐藏电子邮件地址的一部分?

[英]php how can i hide a part of the email adress?

i currently have this: 我目前有这个:

    $value = preg_replace('@hotmail.com', '***********', $value);
    $value = preg_replace('yahoo.com', '***********', $value);
    $value = preg_replace('outlook.com', '***********', $value);
    $value = preg_replace('mailinator.com', '***********', $value);
    $value = preg_replace('live.com', '***********', $value);
    $value = preg_replace('live.nl', '***********', $value);

but how can i show e email adress like this: juliankuit* * ** * *** ? 但是我如何显示这样的电子邮件地址:juliankuit * * ** * *** email is: juliankuitert@hotmail.com 电子邮件是:juliankuitert@hotmail.com

and without having to replace all the email providers like hotmail.com with * 's 而且不必用*替换所有的电子邮件提供商,例如hotmail.com

thanks in advance! 提前致谢!

echo preg_replace('/(?<=.).(?=.*@)/u','*','TestEmail@Google.com');

将返回

T********@Google.com

I'm using this function 我正在使用此功能

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

This an example 这个例子

echo maskEmail('abcdfghi@example.com'); // abcd***@example.com

My PhP is too rusty to put code example, but here's the logic I would use : 我的PhP太生锈了,无法放置代码示例,但这是我将使用的逻辑:

  1. find @ symbol index in string 2) 在字符串2中查找@符号索引)
  2. truncate string to X characters , X being the number found on step 1 minus a certain number of characters (up to you to decide) 将字符串截断为X个字符,X是在第1步中找到的数字减去一定数目的字符(由您决定)
  3. Add a certain amount of * to the string you obtained in step 2 在步骤2中获得的字符串中添加一定数量的*

Note that during step 2, if X <= 0, then the whole string should be replace by * characters 请注意,在步骤2中,如果X <= 0,则整个字符串应替换为*字符

Sometimes its good to show the last character too. 有时也很适合显示最后一个字符

I will suggest you keep things simple. 我建议您保持简单。 Maybe something like this is simple enough https://github.com/fedmich/PHP_Codes/blob/master/mask_email.php 也许这样的事情很简单https://github.com/fedmich/PHP_Codes/blob/master/mask_email.php

Masks an email to show first 3 characters and then the last character before the @ sign 屏蔽电子邮件以显示前3个字符,然后显示@符号前的最后一个字符

ABCDEFZ@gmail.com becomes A*****Z@gmail.com ABCDEFZ@gmail.com成为A*****Z@gmail.com

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM