Php instance: replace the substring

Source: Internet
Author: User
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.