Home > Article > Backend Development > How to replace ID number with asterisk in php
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".
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 ='**************'; $xing2 ='************'; echo substr_replace($str,$xing,0,14);//保留后4位 echo '<br/>'; echo substr_replace($str,$xing2,2,12)//保留前2位和后4位 ?>
Output result:
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!