一款簡單實用的PHP以星號隱藏用戶名手機號碼和郵箱實例,將用戶的一些文字信息隱藏一部分用星號代替,以便於保護用戶隱私。php
隱藏函數:html
function hideStar($str) { //用戶名、郵箱、手機帳號中間字符串以*隱藏 if (strpos($str, '@')) { $email_array = explode("@", $str); $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //郵箱前綴 $count = 0; $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count); $rs = $prevfix . $str; } else { $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i'; if (preg_match($pattern, $str)) { $rs = preg_replace($pattern, '$1****$2', $str); // substr_replace($name,'****',3,4); } else { $rs = substr($str, 0, 3) . "***" . substr($str, -1); } } return $rs; }
測試數據:ide
<?php $account = "sucaihuo.com"; $email = "123456789@qq.com"; $phone = "13800000000"; ?>
以星號*顯示實例:函數
<table width="100%"> <tr> <td>帳號</td> <td>郵箱</td> <td>手機</td> </tr> <tr> <td><?php echo $account;?></td> <td><?php echo $email;?></td> <td><?php echo $phone;?></td> </tr> <tr> <td><?php echo hideStar($account);?></td> <td><?php echo hideStar($email);?></td> <td><?php echo hideStar($phone);?></td> </tr> </table>
就這樣實現了,是否是很簡單!
測試