PHP to implement the replacement of mobile phone number of the median number of * and hidden IP last few methods _php tips

Source: Internet
Author: User
Tags php regular expression

This article describes the PHP implementation of the replacement of mobile phone number between the number of * and hidden IP last few methods. Share to everyone for your reference, specific as follows:

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

Results of output: 138****9549

This match is the result I want, but this match pattern is wrong, it can only match 7, the remaining 4 numbers match not, it shows up, and \$3 does not exist

The correct wording should be

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

Of course, you can also use the method of intercepting a string to hide the middle number

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

Output Result: 138****9549

Hide IP last few for *

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

PS: Here again for you to provide 2 very convenient regular expression tools for your reference to use:

JavaScript Regular expression online test tool:
Http://tools.jb51.net/regex/javascript

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

For more information on PHP related content readers can view the site topics: PHP Regular Expression Usage summary, PHP array operation technique Encyclopedia, PHP Basic Grammar Getting Started tutorial, PHP operation and operator Usage Summary, PHP object-oriented Program design Introductory Course, " PHP Network Programming skills Summary, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.