How to replace the 11 digits after the colon in php

DDD
Release: 2023-06-05 16:49:26
Original
1646 people have browsed it

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.

How to replace the 11 digits after the colon in php

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

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 = &#39;13912345678&#39;;
$output = substr_replace($input, &#39;***********&#39;, 3, 11);
echo $output;         // 输出:139*********
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!