search
HomeBackend DevelopmentPHP TutorialPHP method to implement asterisk to hide username, mobile phone and email address

这篇文章主要介绍了PHP实现星号隐藏用户名,手机和邮箱的方法,涉及php针对字符正则替换的相关操作技巧,需要的朋友可以参考下

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

<?php
function hideStar($str) { //用户名、邮箱、手机账号中间字符串以*隐藏
  if (strpos($str, &#39;@&#39;)) {
    $email_array = explode("@", $str);
    $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
    $count = 0;
    $str = preg_replace(&#39;/([\d\w+_-]{0,100})@/&#39;, &#39;***@&#39;, $str, -1, $count);
    $rs = $prevfix . $str;
  } else {
    $pattern = &#39;/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i&#39;;
    if (preg_match($pattern, $str)) {
      $rs = preg_replace($pattern, &#39;$1****$2&#39;, $str); // substr_replace($name,&#39;****&#39;,3,4);
    } else {
      $rs = substr($str, 0, 3) . "***" . substr($str, -1);
    }
  }
  return $rs;
}
?>
<?php
$account = "jb51.net";
$email = "123456789@qq.com";
$phone = "13888888888";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>演示:PHP以星号隐藏用户名手机和邮箱</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <link rel="stylesheet" type="text/css" href="css/common.css" />
    <style type="text/css">
    </style>
  </head>
  <body>
    <p class="head">
      <p class="head_inner clearfix">
        <ul id="nav">
          <li><a href="/">首 页</a></li>
          <li><a href="/templates">网站模板</a></li>
          <li><a href="/js">网页特效</a></li>
          <li><a href="/php">PHP</a></li>
          <li><a href="/site">精选网址</a></li>
        </ul>
        <a class="logo" href=""><img src="/static/imghwm/default1.png"  data-src="images/logo.jpg"  class="lazy"   alt="素材火logo" /></a>
      </p>
    </p>
    <p class="container">
      <p class="demo">
        <h2 id="a-nbsp-href-教程-PHP以星号隐藏用户名手机和邮箱-a"><a href="#">教程:PHP以星号隐藏用户名手机和邮箱</a></h2>
        <table width="100%" class="table_parameters">
          <tr class="tr_head">
            <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 class="red">
            <td><?php echo hideStar($account); ?></td>
            <td><?php echo hideStar($email); ?></td>
            <td><?php echo hideStar($phone); ?></td>
          </tr>
        </table>
      </p>
    </p>
  </body>
</html>

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP单态模式简单用法

PHP中串行化用法

php实现数据库操作Model类

The above is the detailed content of PHP method to implement asterisk to hide username, mobile phone and email address. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools