Home > Backend Development > PHP Tutorial > Detailed explanation of replacing the middle four digits of PHP mobile phone number with asterisk *

Detailed explanation of replacing the middle four digits of PHP mobile phone number with asterisk *

怪我咯
Release: 2023-03-10 14:40:02
Original
2512 people have browsed it

本篇文章主要介绍了PHP手机号中间四位用星号*代替显示的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在显示用户列表的场景中,一般用到手机号的显示时都需要对手机号进行处理,一般是把中间的四位换成星号****,我本人用php处理的思路是进行替换,用****替换手机号的中间四位

代码如下:

$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']);  //用****进行替换
}
Copy after login

另外几种方法

<?php
$tel = &#39;12345678910&#39;;
//1.字符串截取法
$new_tel1 = substr($tel, 0, 3).&#39;****&#39;.substr($tel, 7);
var_dump($new_tel1);
//2.替换字符串的子串
$new_tel2 = substr_replace($tel, &#39;****&#39;, 3, 4);
var_dump($new_tel2);
//3.用正则
$new_tel3 = preg_replace(&#39;/(\d{3})\d{4}(\d{4})/&#39;, &#39;$1****$2&#39;, $tel);
var_dump($new_tel3);
?>
Copy after login

结果:

> string(11) "123****8910"
> string(11) "123****8910"
> string(11) "123****8910"
Copy after login

The above is the detailed content of Detailed explanation of replacing the middle four digits of PHP mobile phone number with asterisk *. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template