Home > php教程 > php手册 > php 字符串替换为星号或其它字符(手机号,身份证)

php 字符串替换为星号或其它字符(手机号,身份证)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 09:14:26
Original
1096 people have browsed it

在php中字符串中指定位置的字符替换为星号我们有很我函数可以实现如有substr,preg_replace,substr_replace等下面我分别给这三个函数分别介绍一个实例,主要讲到电话,身份证.

手机号码字符串替换为星号代码:

<?php
$str = "15832818835";
echo substr($str,0,3).&#39;*****&#39;.substr($str,8,strlen($str));//保留前三位和后三位
?>
Copy after login

或用正则

<?php 
$s=&#39;www.phprm.com的王经理:13999312365 李经理:13588958741&#39;; 
$s=preg_replace(&#39;#(d{3})d{5}(d{3})#&#39;, &#39;${1}*****${2}&#39;, $s); 
echo $s; 
//王经理:139*****365 李经理:135*****741 
?>
Copy after login

替换字符串中间位置字符为星号

function half_replace($str){  
    $len = strlen($str)/2;  
    return substr_replace($str,str_repeat(&#39;*&#39;,$len),ceil(($len)/2),$len);  
}  
echo half_replace(&#39;test&#39;),"n",half_replace(&#39;tests&#39;),"n",half_replace(&#39;exceptions&#39;);
Copy after login

PHP身份证号打星号

echo strlen($idcard)==15?substr_replace($idcard,"****",8,4):(strlen($idcard)==18?substr_replace($idcard,"****",10,4):"phprm.com提示身份证位数不正常!");
Copy after login


文章地址:

转载随意^^请带上本文地址!

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template