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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-02 09:14:261096Durchsuche

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

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

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

或用正则

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

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

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

PHP身份证号打星号

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


文章地址:

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn