最近项目中遇到这个问题,自己写了一个,利用的是mb_相关的方法,比较简单方便
static function PassStart($str,$start,$end=0,$dot="*",$charset="UTF-8"){
$len = mb_strlen($str,$charset);
if($start==0||$start>$len){
$start = 1;
}
if($end!=0&&$end>$len){
$end = $len-2;
}
$endStart = $len-$end;
$top = mb_substr($str, 0,$start,$charset);
$bottom = "";
if($endStart>0){
$bottom = mb_substr($str, $endStart,$end,$charset);
}
$len = $len-mb_strlen($top,$charset);
$len = $len-mb_strlen($bottom,$charset);
$newStr = $top;
for($i=0;$i<$len;$i++){
$newStr.=$dot;
}
$newStr.=$bottom;
return $newStr;
}
直接调用即可PassStart("", 4,4);
输出结果:
"cardid":"6222***********3655","certifId":"4304**********1011","mobile":"138****6306","name":"张*",