在php中字符串中指定位置的字符替换为星号我们有很我函数可以实现如有substr,preg_replace,substr_replace等下面我分别给这三个函数分别介绍一个实例,主要讲到电话,身份证.
手机号码字符串替换为星号代码:
代码如下复制代码
$str = "15832818835";
echo substr(http://www.111cn.net/tags.php/substr/)
($str,0,3).'*****'.substr($str,8,strlen($str));//保留前三位和后三位
?>
或用正则
$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提示身份证位数不正常!");
网友评论