PHP头条
热点:

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


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

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

 代码如下 复制代码

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

或用正则

<?php
$s='www.111cn.net的王经理:13999312365 李经理:13588958741';
$s=preg_replace('#(d{3})d{5}(d{3})#', '${1}*****${2}', $s);
echo $s;
//王经理:139*****365 李经理:135*****741
?>


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

 代码如下 复制代码

function half_replace($str){ 
   $len = strlen($str)/2; 
    return substr_replace($str,str_repeat('*',$len),ceil(($len)/2),$len); 

 
echo half_replace('test'),"n",half_replace('tests'),"n",half_replace('exceptions');

PHP身份证号打星号

 代码如下 复制代码

echo strlen($idcard)==15?substr_replace($idcard,"****",8,4):(strlen($idcard)==18?substr_replace($idcard,"****",10,4):"111cn.net提示身份证位数不正常!");

www.phpzy.comtrue/php/35312.htmlTechArticlephp 字符串替换为星号或其它字符(手机号,身份证) 在php中字符串中指定位置的字符替换为星号我们有很我函数可以实现如有substr,preg_replace,substr_replace等下面我分别给这三个函数分别...

相关文章

PHP之友评论

今天推荐