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.