Home  >  Article  >  Backend Development  >  How to replace ID number with asterisk in php

How to replace ID number with asterisk in php

WBOY
WBOYOriginal
2021-11-17 11:42:003932browse

In PHP, you can use the substr_replace() function to replace the ID number with an asterisk. The syntax is "substr_replace(ID number string, asterisk string, starting replacement position, replacement string) length);", where the "length of the replacement string" must be the same as the length of the "asterisk string".

How to replace ID number with asterisk in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php implements ID card Method of replacing numbers with asterisks

In PHP, we can use the substr_replace() function to replace asterisks with ID numbers. The substr_replace() function replaces part of a string with another a string.

The syntax of the substr_replace() function is:

substr_replace(string,replacement,start,length)

It should be noted that:

  • The string parameter is required and indicates the regulations The string to be checked; the replacement parameter is also required and specifies the string to be inserted.

  • # The start parameter is required and specifies where to start replacement in the string.

    Positive number - Replace starting at the specified position in the string

    Negative number - Replace starting at the specified position from the end of the string

    0 - Replace at the specified position in the string Start replacing at one character

  • #The length parameter is optional and specifies how many characters to replace. The default is the same as the string length.

    Positive number - the length of the string to be replaced

    Negative number - represents the number of characters from the end of the substring to be replaced to the end of string.

    0 - Insert instead of replace

The example is as follows:

<?php
$str = "341234567891236439";
$xing =&#39;**************&#39;;
$xing2 =&#39;************&#39;;
echo substr_replace($str,$xing,0,14);//保留后4位
echo &#39;<br/>&#39;;
echo substr_replace($str,$xing2,2,12)//保留前2位和后4位
?>

Output result:

How to replace ID number with asterisk in php

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of How to replace ID number with asterisk 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