惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

主题客

网站服务 - 主题客 Zane · WordPress 无边栏高级多功能商用主题 - 主题客 CSS 伪元素 content 属性中文乱码的解决 - 主题客 升级为 PHP 8.0+ 提示 PHP Deprecated 错误的解决办法 - 主题客 niRvana 关注微信公众号扫码登录扩展 - 主题客 Verdure 关注微信公众号扫码登录扩展 - 主题客 在线编辑图片WebP格式转换图片压缩小技巧 - 主题客 解决 WordPress 5.8+ 无法上传 webp 图片的问题 - 主题客 WordPress 5.8 禁用小工具区块编辑模式 - 主题客
保护隐私信息,使用星号隐藏手机号码、邮箱和用户名 - 主题客
主题客 · 2022-12-07 · via 主题客

我们在做项目的时候,可能会遇到需要在有些用户基本资料显示的地方,使用一些技术手段来保护用户的隐私信息。例如用户的邮箱地址、用户名或者是用户的手机号码。那么我们最常用的方式方法就是使用(*)星号来处理这些隐私信息的显示效果以达到保护用户隐私信息的目的。

下面就来说说一种简单又实用的使用(*)星号隐藏用户信息的方法,我已经将代码封装,支持邮箱地址、手机号码、中文用户名,具体行有注释,复制下面代码到自己项目中直接用就好了。如果是使用WordPress的话,复制代码至你自己主题的function.php中就可以在需要的地方直接调用了,很方便。

/**
 * 使用(*)星号隐藏手机号码、中文用户名及邮箱地址
 * @Author   Jackie
 * @Author url   https://www.themeke.com
 */
function starStr($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);  //邮箱前缀星号替换
        $repstr = $prevfix . $str;
    } else {
        $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';   //正则判断手机号
        if (preg_match($pattern, $str)) { //手机号码星号替换
            $repstr = preg_replace($pattern, '$1****$2', $str); 
        } else { //中文用户名星号替换
          	$strlen = mb_strlen($str, 'utf-8');
            $firstStr = mb_substr($str, 0, 1, 'utf-8');
            $lastStr = mb_substr($str, -1, 1, 'utf-8');
            if ($strlen == 2){
                $repstr = $firstStr . str_repeat('*', mb_strlen($str, 'utf-8') - 1);
            }else{
                $repstr = $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
            } 
        }
    }
    return $repstr;
}
<?php 
$username = "用户名"; 
$email = "123456@qq.com"; 
$phone = "13512345678"; 

echo starStr($username);  // 输出: 用*名
echo starStr($email);  // 输出: 123***@qq.com
echo starStr($phone);  // 输出: 135****5678
?>