温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP隐藏手机号码中间4位的示例

发布时间:2021-02-20 09:14:55 来源:亿速云 阅读:178 作者:小新 栏目:编程语言

这篇文章主要介绍PHP隐藏手机号码中间4位的示例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

方法演示

1.使用 substr_replace函数

# substr_replace — 替换字符串的子串
# 使用说明
substr_replace ( mixed $string , mixed $replacement , mixed $start , mixed $length = ? ) : mixed
# $string 资源字符串
# $replacement 替换字符
# $start 替换开始位置,如果位负数的时候,将从末尾开始数
# $length 需要替换的长度,如果为负数的时候,也是从$start开始位置替换
# substr_replace() 在字符串 string 的副本中将由 start 和可选的 length 参数限定的子字符串使用 replacement 进行替换。
# 示例
$mobile = '18512341234';
echo substr_replace($mobile, '****', 3, 4);         // 185****1234
# 注意 字符串的开始位置为0
echo substr_replace($mobile, '****', -8, -4);    // 185****1234

2.使用正则表达式

# preg_replace — 执行一个正则表达式的搜索和替换
# 使用说明
preg_replace ( mixed $pattern , mixed $replacement , mixed $subject , int $limit = -1 , int &$count = ? ) : mixed
# 搜索 subject 中匹配 pattern 的部分,以 replacement 进行替换。

# 示例
$pattern = '/(\d{3})\d{4}(\d{4})/';
$new_mobile = preg_replace($pattern, '$1****$2', $mobile);
echo $new_mobile;

3.使用 substr函数

# 函数说明
substr ( string $string , int $start , int $length = ? ) : string
# 返回字符串 string 由 start 和 length 参数指定的子字符串。   
# 同 substr_replace 一样,start也可以为负数的
# 示例
echo substr($mobile, 0,3) . '****' . substr($mobile, 7,4);
echo substr($mobile, 0,3) . '****' . substr($mobile, -4,4);

以上是“PHP隐藏手机号码中间4位的示例”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI