PHP使用星号替代用户名手机和邮箱

PHP使用星号替代用户名手机和邮箱这个在许多的活动界面会看到如淘宝的购物界面中的一些客户的支付宝号都是隐藏掉的哦,下面我们来看一下它的使用方法吧.

  1. <?php
  2. function hideStar($str) { //用户名、邮箱、手机账号中间字符串以*隐藏
  3. if (strpos($str, '@')) {
  4. $email_array = explode("@", $str);
  5. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
  6. $count = 0;
  7. $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
  8. $rs = $prevfix . $str;
  9. } else {
  10. $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';
  11. if (preg_match($pattern, $str)) {
  12. $rs = preg_replace($pattern, '$1****$2', $str); // substr_replace($name,'****',3,4);
  13. } else {
  14. $rs = substr($str, 0, 3) . "***" . substr($str, -1);
  15. }
  16. }
  17. return $rs;
  18. }
  19. ?>
  20. <?php
  21. $account = "phpfensi.com";
  22. $email = "416148489@qq.com";
  23. $phone = "18005152525";
  24. ?>
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  26. <html xmlns="http://www.w3.org/1999/xhtml">
  27. <head>
  28. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  29. <title>演示:PHP以星号隐藏用户名手机和邮箱</title>
  30. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
  31. <link rel="stylesheet" type="text/css" href="http://www.phpfensi.com /jquery/css/common.css" />
  32. <style type="text/css">
  33. </style>
  34. </head>
  35. <body>
  36. <div class="head">
  37. <div class="head_inner clearfix">
  38. <ul >
  39. <li><a href="http://www.phpfensi.com ">首 页</a></li>
  40. <li><a href="http://www.phpfensi.com /templates">网站模板</a></li>
  41. <li><a href="http://www.phpfensi.com /js">网页特效</a></li>
  42. <li><a href="http://www.phpfensi.com /php">PHP</a></li>
  43. <li><a href="http://www.phpfensi.com /site">精选网址</a></li>
  44. </ul>
  45. <a class="logo" href="http://www.phpfensi.com "><img src="http://www.phpfensi.com /Public/images/logo.jpg" alt="素材火logo" /></a>
  46. </div>
  47. </div>
  48. <div class="container">
  49. <div class="demo">
  50. <h2 class="title"><a href="http://www.phpfensi.com /js/548.html">教程:PHP以星号隐藏用户名手机和邮箱</a></h2>
  51. <table width="100%" class="table_parameters">
  52. <tr class="tr_head">
  53. <td>账号</td>
  54. <td>邮箱</td>
  55. <td>手机</td>
  56. </tr>
  57. <tr>
  58. <td><?php echo $account; ?></td>
  59. <td><?php echo $email; ?></td>
  60. <td><?php echo $phone; ?></td>
  61. </tr>
  62. <tr class="red">
  63. <td><?php echo hideStar($account); ?></td>
  64. <td><?php echo hideStar($email); ?></td>
  65. <td><?php echo hideStar($phone); ?></td>
  66. </tr>
  67. </table>
  68. </div>
  69. </div>
  70. </body>
  71. </html>