Php implements the method to replace the number * in the middle of the mobile phone number and hide the last digit of the IP address. The mobile phone number ip Address

Source: Internet
Author: User

Php implements the method to replace the number * in the middle of the mobile phone number and hide the last digit of the IP address. The mobile phone number ip Address

This example describes how to replace the number * in the middle of the mobile phone number in php and how to hide the last digit of the IP address. We will share this with you for your reference. The details are as follows:

$string = "13826589549";$pattern = "/(\d{3})\d\d(\d{2})/";$replacement = "\$1****\$3";print preg_replace($pattern, $replacement, $string);

Output result: 138 **** 9549

The matching result is what I want, but the matching mode is incorrect. It can only match 7 digits, and the remaining 4 digits cannot match, and \ $3 does not exist at all.

Which of the following statements is true?

$string = "13826589549";$pattern = "/(\d{3})\d{4}(\d{4})/";$replacement = "\$1****\$2";print preg_replace($pattern, $replacement, $string);

You can also use the string truncation method to hide numbers in the middle.

function suohao($phone){ $p = substr($phone,0,3)."****".substr($phone,7,4); return $p;}echo suohao($string);

Output: 138 **** 9549

The last number of hidden IP addresses is *

<?php echo preg_replace("/[^\.]{1,3}$/","*",$ip); ?>

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

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.