The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer.
In the work of the past few days, the prototype of the "Bank Card" page is as follows. Among them, only the last four digits of the card numbers of different bank cards can be displayed, and other numbers are hidden with * characters.
After communicating with the front-end, I will hide the numbers. This uses the string replacement substr_replace function in PHP.
#PHP string replacement, as the name suggests, is used to replace a specified string from a string.
Related functions are as follows:
substr_replace()——Replace part of a string with another string
str_replace() ——Use a string to replace other characters in the string
1. substr_replace()
Syntax:
substr_replace(string,replacement,start,length)
Description | |
---|---|
string | Required. Specifies the string to check. |
replacement | Required. Specifies the string to be inserted. |
start |
Required. Specifies where in the string to begin replacement.
|
Optional. Specifies how many characters to replace. The default is the same as the string length.
|
|
Return the replaced string
|
##2. str_replace( )
Syntax:
str_replace(find,replac,string,count)
find | |
---|---|
Required. Specifies the value to look for. | replace |
##Required. Specifies the value to replace the value in find. | string |
Required. Specifies the string to be searched for. | count |
Optional. Variable counting the number of substitutions. | Return value |
Return a string or array with replacement value | The above are the basic knowledge points of string replacement substr_replace() and str_replace() functions, so in actual work, I used the first one-substr_replace() function. [Recommended: PHP Video Tutorial] After inquiry, I learned that the current domestic bank cards are 16 or 19 digits in length, so I checked from the data table After getting the bank card number, first use the strlen() function to count the length of the string. When the bank card is 16 digits, the first 12 digits are replaced by 12 *, that is, the replacement is ************; When the bank card When it is a 19-digit number, the first 15 digits are replaced by 15 *, that is, the replacement is ****************; The code is as follows: The test results are as follows: |