一款简单实用的PHP以星号隐藏用户名手机号码和邮箱实例,将用户的一些文字信息隐藏一部分用星号代替,以便于保护用户隐私。

隐藏函数:
  1. function hideStar($str) { //用户名、邮箱、手机账号中间字符串以*隐藏
  2.     if (strpos($str, '@')) {
  3.         $email_array = explode("@", $str);
  4.         $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
  5.         $count = 0;
  6.         $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
  7.         $rs = $prevfix . $str;
  8.     } else {
  9.         $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';
  10.         if (preg_match($pattern, $str)) {
  11.             $rs = preg_replace($pattern, '$1****$2', $str); // substr_replace($name,'****',3,4);
  12.         } else {
  13.             $rs = substr($str, 0, 3) . "***" . substr($str, -1);
  14.         }
  15.     }
  16.     return $rs;
  17. }

测试数据:
  1. <?php
  2. $account = "sucaihuo.com";
  3. $email = "[email protected]";
  4. $phone = "13800000000";
  5. ?>

以星号*显示实例:
  1. <table width="100%">
  2.     <tr>
  3.         <td>账号</td>
  4.         <td>邮箱</td>
  5.         <td>手机</td>
  6.     </tr>
  7.     <tr>
  8.         <td><?php echo $account;?></td>
  9.         <td><?php echo $email;?></td>
  10.         <td><?php echo $phone;?></td>
  11.     </tr>
  12.     <tr>
  13.         <td><?php echo hideStar($account);?></td>
  14.         <td><?php echo hideStar($email);?></td>
  15.         <td><?php echo hideStar($phone);?></td>
  16.     </tr>
  17. </table>
本文转自:https://www.sucaihuo.com/php/548.html 转载请注明出处!
 
10-05 19:36