In this article, the example of php to achieve the replacement of the middle digits of the cell phone number as * and hide the last few bits of the IP method. Shared for your reference, as follows:
$string = "13826589549"; $pattern = "/(\d{3})\d\d(\d{2})/"; $replacement = "\$1****\$3"; print preg_replace($pattern, $replacement, $string);
Output: 138****9549
This match result is what I want, but this match pattern is wrong, it can only match 7, the remaining 4 numbers can't be matched, it shows up, and \$3 doesn't exist!
The correct way to write this would 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 the string to hide the middle number
function suohao($phone){ $p = substr($phone,0,3)."****".substr($phone,7,4); return $p; } echo suohao($string);
Output: 138****9549
Hide the last digits of the IP as *
<?php echo preg_replace("/[^\.]{1,3}$/","*",$ip); ?>
PS: Here are 2 more very convenient regular expression tools for your reference:
JavaScript regular expression online test tool:
http://tools./regex/javascript
Regular expression online generation tool:
http://tools./regex/create_reg
Readers interested in more PHP-related content can check out this site's topic: thephp regular expression usage summary》、《PHP array (Array) operation skills of the book》、《PHP basic syntax tutorial》、《PHP Operations and Operators Usage Summary》、《php object-oriented programming tutorial for beginners》、《PHP Web Programming Tips Summary》、《php string (string) usage summary》、《php + mysql database operation tutorial for beginnersand thephp summary of common database operation techniques》
I hope that what I have said in this article will help you in PHP programming.