Home  >  Article  >  Backend Development  >  How to replace the 11 digits after the colon in php

How to replace the 11 digits after the colon in php

PHPz
PHPzOriginal
2023-03-21 16:32:221354browse

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.

In this article, we will learn how to replace the 11 digits after the colon.

First of all, why do we need to replace the 11 digits after the colon? In actual development, there may be some data that needs to be protected, and the mobile phone number is sensitive information. In order to protect the user's privacy, we will process or encrypt certain numbers in the mobile phone number. A common way to do this is to replace the mobile phone number with a colon, keeping the first three and last two digits, such as: 139**9527.

Now, let’s take a look at how to use PHP to replace the 11 digits after the colon.

Method 1: Use regular expression replacement

PHP provides a very powerful regular expression function preg_replace(), which can match the string of specified rules and replace it. The following is a code example that uses preg_replace() to replace the 11 digits after the colon:

<?php
$input = &#39;13912345678&#39;;
$output = preg_replace(&#39;/(?<=:\b)[0-9]{11}/&#39;, &#39;***********&#39;, $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:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn