PHP uses asterisk instead of user name mobile phone and mailbox implementation code

  • 2021-09-11 19:50:31
  • OfStack

PHP uses asterisks instead of user names, mobile phones and mailboxes. In many active interfaces, you will see that the Alipay numbers of some customers in Taobao's shopping interface are hidden. Let's take a look at how to use it.

function hideStar($str) { // The middle string of user name, email address and mobile phone account is used to * Hide  
  if (strpos($str, '@')) { 
    $email_array = explode("@", $str); 
    $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); // Mailbox prefix  
    $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; 
$account = ""; 
$email = ""; 
$phone = "18005152525"; 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> 
<html xmlns=""> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title> Demo: PHP Hide username, mobile phone and mailbox with asterisk </title> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/> 
    <link rel="stylesheet" type="text/css" href=" /jquery/css/common.css" rel="external nofollow" /> 
    <style type="text/css"> 
    <div class="head"> 
      <div class="head_inner clearfix"> 
        <ul id="nav"> 
          <li><a href=" " rel="external nofollow" rel="external nofollow" > Head   Page </a></li> 
          <li><a href=" /templates" rel="external nofollow" > Site template </a></li> 
          <li><a href=" /js" rel="external nofollow" > Web special effects </a></li> 
          <li><a href=" /php" rel="external nofollow" >PHP</a></li> 
          <li><a href=" /site" rel="external nofollow" > Selected Web sites </a></li> 
        <a class="logo" href=" " rel="external nofollow" rel="external nofollow" ><img src=" /Public/images/logo.jpg" alt=" Material fire logo" /></a> 
    <div class="container"> 
      <div class="demo"> 
        <h2 class="title"><a href=" /js/548.html" rel="external nofollow" > Tutorials: PHP Hide username, mobile phone and mailbox with asterisk </a></h2> 
        <table width="100%" class="table_parameters"> 
          <tr class="tr_head"> 
            <td> Account number </td> 
            <td> Mailbox </td> 
            <td> Mobile phone </td> 
            <td><?php echo $account; ?></td> 
            <td><?php echo $email; ?></td> 
            <td><?php echo $phone; ?></td> 
          <tr class="red"> 
            <td><?php echo hideStar($account); ?></td> 
            <td><?php echo hideStar($email); ?></td> 
            <td><?php echo hideStar($phone); ?></td> 


Related articles: