一款簡單實用的PHP以星號隱藏用戶名手機號碼和郵箱實例,將用戶的一些文字信息隱藏一部分用星號代替,以便於保護用戶隱私。php
隱藏函數:html
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 }
測試數據:ide
1 <?php 2 $account = "sucaihuo.com"; 3 $email = "123456789@qq.com"; 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 轉載請註明出處!測試