Home >Backend Development >PHP Problem >How to replace the 11 digits after the colon in php
php method to replace the 11 digits after the colon: 1. Use regular expression to replace, use the regular expression "/ (?<=:\b)[0-9]{11}/" to match 11 digits, and then use the "preg_replace()" function to replace the matched 11 digits with "*"; 2. Use substr_replace() to replace, use "substr_replace($input, '************ *', 3, 11)" syntax is used to replace the 11 digits after the colon.
The operating environment of this tutorial: Windows 10, php8.1.3 version, dell g3 computer.
Replacing a string is one of the most basic operations in PHP. The replacement function is crucial in data processing and string operations, and can improve program execution efficiency and performance. There are many functions for replacing strings in PHP, such as: substr_replace, str_replace, preg_replace and so on. But for specific string replacements, special replacement rules may need to be used.
php method to replace the 11 digits after the colon:
Method 1: Use regular expression to replace
PHP provides a very powerful Regular expression function preg_replace(), which can match strings of specified rules and replace them. The following is a code example that uses preg_replace() to replace the 11 digits after the colon:
<?php $input = '13912345678'; $output = preg_replace('/(?<=:\b)[0-9]{11}/', '***********', $input); echo $output; // 输出:139********* ?>
The above code uses a regular expression, / (?<=:\b)[0-9 ]{11}/, its specific meaning is: match the 11 digits after the colon. Among them, the regular expression (?<=:\b) is a zero-width assertion, which means matching the position after the colon. [0-9]{11} means matching 11 numbers. Finally, use the preg_replace() function to replace the matched 11 numbers with "*".
Method 2: Use substr_replace() to replace
In addition to using regular expressions, you can also use the substr_replace() function to replace the 11 digits after the colon. The substr_replace() function is one of the more commonly used functions in PHP string operations. It can replace strings according to specified rules.
The following is a code example of using substr_replace() to replace the 11 digits after the colon:
<?php $input = '13912345678'; $output = substr_replace($input, '***********', 3, 11); echo $output; // 输出:139********* ?>
In the above code, the first parameter of the substr_replace() function is to be replaced. String, the second parameter is used to replace the text at the specified position in the replacement string, the third parameter is the starting position of the replacement position, and the fourth parameter is the length of the replacement character. Because there are 11 digits to be replaced after the colon, the starting position of the replacement position is 3, and the length of the replacement character is 11.
To sum up, the above are two ways to replace the 11-digit number after the colon in PHP. The requirements can be achieved using regular expressions or the substr_replace() function, and the choice should be based on specific business needs.
The above is the detailed content of How to replace the 11 digits after the colon in php. For more information, please follow other related articles on the PHP Chinese website!