Home >Backend Development >PHP Tutorial >How to replace the middle four digits of PHP mobile phone number with asterisks *

How to replace the middle four digits of PHP mobile phone number with asterisks *

墨辰丷
墨辰丷Original
2018-05-22 09:20:043161browse

本篇文章主要介绍了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']);  //用****进行替换
}

另外几种方法

<?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);
?>

结果:

> string(11) "123****8910"
> string(11) "123****8910"
> string(11) "123****8910"

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关推荐:

php中如何利用正则判断是否是手机号码

php验证输入的手机号码是否合法

PHP之cURL实现手机号码归属地查询功能

The above is the detailed content of How to replace the middle four digits of PHP mobile phone number with asterisks *. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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