php手机号中间变星号****如何实现

php手机号中间变星号****如何实现的几种方法:
 
<?php
$tel = '12345678910';
//1.字符串截取法
$new_tel1 = substr($tel, 0, 3).'****'.substr($tel, 7);
var_dump($new_tel1);
//2.替换字符串的子串
$new_tel2 = substr_replace($tel, '****', 3, 4);
var_dump($new_tel2);
//3.用正则
$new_tel3 = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
var_dump($new_tel3);
?>
结果如下:
 
> string(11) "123****8910"
> string(11) "123****8910"
> string(11) "123****8910"
定义函数循环的方法:
 
$all_lottery_logs = ********;     //该语句是得到中奖纪录
//遍历处理手机号
foreach($all_lottery_logs as $k=>$v){
$xing = substr($v['tel'],3,4); //获取手机号中间四位
$all_lottery_logs[$k]['tel'] = str_replace($xing,'****',$v['tel']); //用****进行替换
}

0 个评论

要回复文章请先登录注册