Mobile phone number middle part replace Chengshing number

Source: Internet
Author: User

Today wrote a page, there is an effect, mobile phone number in the middle of 4 digits is replaced by the asterisk, this effect is very simple, the implementation of a variety of methods, can be a string interception can also use regular ^_^

The effects to be achieved are as follows:

135****2627js Code: Method ①: String intercept + stitching
   var phone = ' 13500001111 ';  Phone.substr (0, 3) + ' * * * * + PHONE.SUBSTR (7, 11);

  

The principle of this method is to first intercept the first 3 characters of the string, stitching the middle of the 4 * number, and then intercept the string after the 4-bit character. The substring () function is also possible oh.

The above is written as a method that can be reused later.

function Formatphone (phone) {       return phone.substr (0, 3) + ' * * * * + PHONE.SUBSTR (7, one);  }           

  

In the future need to call the time to transfer the phone number in it.

Note: The type of phone is string oh, why do you want to pass in the numbers directly? Well, that's a little bit of a change.

function Formatphone (phone) {            if (typeof phone = = ' number ') {                phone = phone.tostring ();            }            Return Phone.substr (0, 3) + ' * * * * + PHONE.SUBSTR (7, one);        }

  

Determine whether the type of the parameter is numeric, yes to the string type, if you want to be more rigorous, you can judge the other types, to ensure that the string type to execute the code.

Do you think the method ① compare water? Then use the regular outfit to force it.

Method ②: Regular expression substitution
function Formatphone (phone) {            return Phone.replace (/(\d{3}) \d{4} (\d{4})/, "$1****$2");        }

  

The principle is to match the first 3 digits, match the value to $ $, then match the median 4 digits, and then match the last 4 digits to $ A.

A child's shoes that you don't know can be poked here. "Simple introduction to Regular expressions"

Summary: The middle 4 to replace the * number, why is not the other location, first you need to know the mobile phone number rules, China's mobile phone number is 11-bit, the first 3 digits is the number segment, the Middle 4 is the region code, the last 4 bits is the user code. It doesn't matter how many digits you want to replace, at least let people know it's a mobile number, but replacing the middle 4 is a conventional one.

Php:

And, by the way, how to replace it in PHP.

<?php            $phone = "13511112222";            $pattern = "/(\d{0,3}) \d{4} (\d{4})/";            $replacement = "\$1****\$2";            Print Preg_replace ($pattern, $replacement, $phone);        ? >                            <?php            $phone = "13264309555";            Echo Substr_replace ($phone, "* * *", 3, 4);        ? >                            <?php            echo substr ($phone, 0, 3). " ". substr ($phone, 7, 4);        ? >

  

                  
Smarty:
<{' 13511112222 ' |regex_replace: "/(\d{0,3}) \d{4} (\d{4})/": "\$1****\$2"}>

  

Summary: The methods listed above can not only replace the mobile phone number, such as social Security number, phone number, IP, etc., even the user name these are can, but just to have to write about it, here is not said, there is time to write an example out.

Original address: http://www.mingsixue.com/blog/effect/JS-phone-to-asterisk.html

The middle part of the phone number replaces the Chengshing number

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.