php、javascript隐藏手机号函数

  • 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
  • php、javascript隐藏手机号函数

    上一篇:iOS火焰动画效果、图文混排框架、StackView效果、偏好设置、底部手势等源码


    下一篇:手机前端开发调试利器 – vConsole