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

正则表达式:

# 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, &
路是自己走出来的,而不是选出来的。
原文地址:https://www.cnblogs.com/mo3408/p/15741765.html