Question: You want to replace a substring with another string. For example, you want to blur the last few digits of an ID card number before printing
Question: You want to replace a substring with another string. For example, you want to blur the last few digits of an ID card number before printing.
Solution: Use the substr_replace () function
Function: mixed substr_replace (mixed $ string, string $ replacement, int $ start [, int $ length])
Substr_replace () replace the replacement with the substring limited by the start and optional length parameters in the string copy.
$ Uid = '000000'; echo substr_replace ($ uid, 'xxxx', strlen ($ uid)-4 );
Conclusion: If start is a positive number, replacement starts from the start position of string. If start is a negative number, replacement starts from the start position in the penultimate position of string.
If this parameter is set and it is a positive number, it indicates the length of the substring to be replaced in the string. If it is set to a negative number, it indicates the number of characters between the end of the substring to be replaced and the end of the string. If this parameter is not provided, strlen (string) is used by default ). Of course, if the length is 0, the function is to insert the replacement to the start position of the string.