How to replace the character in the middle of a string

Source: Internet
Author: User
How can I replace a character in the middle of a string? how can I replace a character in the middle of a string?
For example, fdsfdsfdsjkww
Replace with: f *********** w
For example, the score is deducted by Jin Ximin.
Replace with: buckle *** people
For example, the score is deducted by Jin Ximin s.
Replace with: *** s
For example, tt points are deducted by Jin Ximin s.
Replace with: t ***** s


Reply to discussion (solution)

  

Replace (string1, start, string2, n, m)

    

Garbled characters, see?

Garbled code depends on what encoding is set correctly.

$ String = 'fdsfdsfdsjkww ';
Echo substr ($ string,). '*****'. substr ($ string, $ sLeng-1, 1 );


     

Garbled characters, see?

If utf8-encoding is used, UTF-8 is used for source file encoding and output display encoding.

Simple

$string='fdsfdsfdsjkww';echo substr_replace($string, str_repeat('*',strlen($string)-2), 1, strlen($string)-2);

Pay attention to the length issue. in different encodings, the Chinese and English bytes will be different. most of them should be correct in length; otherwise, garbled characters will occur.

$string='fdsfdsfdsjkww';echo substr_replace($string, str_repeat('*',strlen($string)-2), 1, strlen($string)-2);

This is relatively simple

Substr_replace? Replace the substring of a string

String
Input string.

Replacement
Replace the string.

Start
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.

Length
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 length is 0, this function is used to insert replacement to the start position of 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.