$string = "13826589549";
$pattern = "/(\d{3})\d\d(\d{2})/";
$replacement = "\$1****\$3";
$pattern = "/(\d{3})\d\d(\d{2})/";
$replacement = "\$1****\$3";
print preg_replace($pattern, $replacement, $string);
输出的结果:138****9549
这个匹配结果是我想要的,但是这个匹配模式是错误的,它只能匹配7个,剩余4个数字匹配不到,就显示出来了,还有\$3根本就不存在
正确的写法应该是
$string = "13826589549";
$pattern = "/(\d{3})\d{4}(\d{4})/";
$replacement = "\$1****\$2";
print preg_replace($pattern, $replacement, $string);
当然还可以使用截取字符串的方法,隐藏中间的数字
function suohao($phone){
$p = substr($phone,0,3)."****".substr($phone,7,4);
return $p;
}
echo suohao($string);
输出结果:138****9549
隐藏IP最后几位为*
<?php echo preg_replace("/[^\.]{1,3}$/","*",$ip); ?>