- php:
a. 代码:
/*
* @desc:隐藏手机号
* @param:tel 手机号,如:13987654321
* @param:num 隐藏的位数,默认4位
* @return ret 回显的手机号,如:139xxxx4321
*/
function hidetel($tel,$num = 4){
$len = mb_strlen($tel);
$start = floor(($len - $num) / 2);
$end = ceil(($len - $num) / 2);
$head = mb_substr($tel,0,$start);
$body = str_repeat('x',$num);
$foot = mb_substr($tel,($start + $num),$end);
$ret = $head.''.$body.''.$foot;
return $ret;
}
b. 测试:
$ret = hidetel(13987654321);
var_dump($ret);
c. 输出:
string(11) "139xxxx4321"
- javascript:
a. 代码:
/*
* @desc:隐藏手机号
* @param:tel 手机号,如:13987654321
* @param:num 隐藏的位数,默认4位
* @return ret 回显的手机号,如:139xxxx4321
*/
function hidetel(tel,num = 4){
tel = tel.toString()
var len = tel.length
var start = Math.floor((len - num) / 2)
var end = Math.ceil((len - num) / 2)
var head = tel.slice(0,start)
var arr = new Array(num + 1)
var body = arr.join('x')
var foot = tel.slice((start + num),len)
var ret = head+''+body+''+foot
return ret
}
b. 测试:
var ret = hidetel(13987654321)
console.log(ret)
c. 输出:
139xxxx4321
|