PHP小技巧

1、手机/电话号码保留前三位和后四位,中间的用*代替

//{:substr_replace($tel, '****',3, 4)}
例如:
<?php
$tel = '1234567890';
//1.字符串截取法
$new_tel1 = substr($tel, 0, 3).'****'.substr($tel, 7);
dump($new_tel1);
//2.替换字符串的子串
$new_tel2 = substr_replace($tel, '****', 3, 4);
dump($new_tel2);
//3.用正则
$new_tel3 = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $tel);
dump($new_tel3);
?>
输出结果:
string(11) "123****8910"
string(11) "123****8910"
string(11) "123****8910"

 

posted @ 2020-11-12 17:03  十三月凉  阅读(116)  评论(0编辑  收藏  举报
/* 看板娘 */