Home  >  Article  >  Backend Development  >  How to hide the ID number in the middle in php

How to hide the ID number in the middle in php

藏色散人
藏色散人Original
2021-03-12 09:11:393256browse

php将身份证号码中间隐藏的实现方法:首先创建一个PHP示例文件;然后通过正则表达式“preg_replace($pattern, $replacement, $string);”方式实现将身份证号码中间用星号替换隐藏即可。

How to hide the ID number in the middle in php

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php实现替换手机号中间数字为*号及隐藏IP最后几位的方法

 

本文实例讲述了php实现替换手机号中间数字为*号及隐藏IP最后几位的方法。分享给大家供大家参考,具体如下:

$string = "13826589549";
$pattern = "/(\d{3})\d\d(\d{2})/";
$replacement = "\$1****\$3";
print preg_replace($pattern, $replacement, $string);

输出的结果:138****9549

这个匹配结果是我想要的,但是这个匹配模式是错误的,它只能匹配7个,剩余4个数字匹配不到,就显示出来了,还有\$3根本就不存在

正确的写法应该是

$string = "13826589549";
$pattern = "/(\d{3})\d{4}(\d{4})/";
$replacement = "\$1****\$2";
print preg_replace($pattern, $replacement, $string);

当然还可以使用截取字符串的方法,隐藏中间的数字

function suohao($phone){
 $p = substr($phone,0,3)."****".substr($phone,7,4);
 return $p;
}
echo suohao($string);

输出结果:138****9549

隐藏IP最后几位为*

<?php echo preg_replace("/[^\.]{1,3}$/","*",$ip); ?>

【推荐学习:《PHP视频教程》】

The above is the detailed content of How to hide the ID number in the middle in php. 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
Previous article:How to use php ziparchiveNext article:How to use php ziparchive