正則表達式方法
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
- ?>
不用正則表達式實現
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
- ?>
新聞熱點
疑難解答