在以下几个示例中,用preg_replace函数实现手机号码中间指定几位替换成星号。
一、使用php正则表达式替换手机号码中间几位。
1、字符串中包含多个手机号码
<?php
$s='王经理:13999312365 李经理:13588958741';
$s=preg_replace('#(d{3})d{5}(d{3})#', '${1}*****${2}', $s);
echo $s;
//王经理:139*****365 李经理:135*****741
?>
2、字符串中只有一个手机号码
<?php
$haoma="15012345678";
echo preg_replace("/(d{3})d{5}/","$1*****",$haoma);
//150*****678
?>
二、非php正则方式替换手机号中间几位
1、使用substr_replace字符串部分替换函数
<?php $string1="13264309555"; echo substr_replace($string1,'*****',3,5); //132*****555 ?>
2、使用字符串截取函数substr
<?php echo substr($string1,0,3)."*****".substr($string1,8,3); //132*****555 ?>