php 手机加*星 【字符串】

场景:展示手机号时,避免暴露隐私信息,因此需要给手机号加*号

代码

php 复制代码
/**
 * 手机号码隐私加星
 * @param string $mobile 手机号
 */
function mobileToStar(string $mobile)
{   
    // 正则检测手机号
    if(!preg_match('/^1[3456789]\d{9}$/',$mobile)){
        // 手机格式错误
        return false;
    }
    // 粗暴法
    // $mobile[4] = "*";
    // $mobile[5] = "*";
    // $mobile[6] = "*";
    // $mobile[7] = "*";
    // return $mobile;
    
    // 截取替换法
    // $mobile_arr = str_split($mobile , 4); // 4个字符  1个字符串,切割 最后长度3
    // $mobile_arr[1] =  '****'; // 把中间4个用* 代替
    // return implode($mobile_arr);

    // 直接替换法
    // return substr_replace($mobile , '****' , 4,4);

    // 字符串截取法
    $start_str = substr($mobile , 0 , 4);// 从0位开始截取,取4个字符
    $in_str = str_pad('',4,'*'); // 设置 * 号
    $end_str = substr($mobile , 8 , 4); // 结束部分  从第8位开始截取,取4个
    return $start_str . $in_str. $end_str;
}

输出

php 复制代码
var_dump(mobileToStar('13112346458'));
// string(11) "1311****458"
相关推荐
真正的醒悟1 小时前
图解网络22
服务器·网络·php
云梦谭4 小时前
AI 生成的FreeSWITCH 呼出流程深度分析freeswitch-1.10.12.-release
java·前端·php
上海云盾-小余5 小时前
DDoS 攻击溯源:DNS 水印标记 + 区块链存证的双保险
区块链·php·ddos
Web极客码6 小时前
CentOS与RHEL安装EPEL源解析错误修复
linux·centos·php
catchadmin6 小时前
如何创建和使用 Shell 脚本实现 PHP 部署自动化
开发语言·自动化·php
杰哥技术分享6 小时前
宿主机(CentOS)没有安装 PHP,但想使用php
linux·centos·php
m0_738120726 小时前
渗透测试——靶机Sar1渗透横向详细过程
开发语言·python·安全·web安全·网络安全·ssh·php
济南壹软网络科技有限公司7 小时前
壹软V4Max 旗舰版:定义新一代盲盒电商生态系统
php·盲盒源码·盲盒h5·国际盲盒源码
BingoGo7 小时前
万物皆字符串 PHP 中的原始类型偏执
后端·php
网络小白不怕黑8 小时前
IPv6核心技术与NDP协议深度解析:从地址架构到邻居发现
开发语言·架构·php