PHP, JavaScript hidden phone number function

Source: Internet
Author: User

  • Php:
    A. Code:
    /** @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. Testing:

    $ret = hidetel(13987654321);var_dump($ret);

    C. Output:

    string(11) "139xxxx4321"
  • Javascript:
    A. Code:
    /** @desc:隐藏手机号* @param:tel 手机号,如:13987654321* @param:num 隐藏的位数,默认4位* @return ret 回显的手机号,如:139xxxx4321*/function hidetel(tel,num = 4){tel = tel.toString()var len = tel.lengthvar 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+‘‘+footreturn ret}

    B. Testing:

    var ret = hidetel(13987654321)console.log(ret)

    C. Output:

    139xxxx4321
  • PHP, JavaScript hidden phone number function

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.