How to replace a character in the middle of a string

Source: Internet
Author: User
How do I replace a character in the middle of a string?
For example: Fdsfdsfdsjkww
Replaced by: f**********w
For example: deduction of Kim Simin
To be replaced by: Buckle * * * min
For example: Deduction of Kim Simin s
To be replaced by: Buckle ****s
For example: TT buckle Kim Simin s
Replaced by: t*****s


Reply to discussion (solution)

  

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

    

It's garbled, see?

Garbled to see what your code is set the correct encoding

$string = ' Fdsfdsfdsjkww ';
Echo substr ($string, 0, 1). ' '. substr ($string, $sLeng -1,1);


     

It's garbled, see?

If you use utf8-encoding, the encoding of the source file and the encoding of the output display are used Utf-8

Simple point of

$string = ' Fdsfdsfdsjkww '; Echo substr_replace ($string, Str_repeat (' * ', strlen ($string)-2), 1, strlen ($string)-2);

Note the length of the problem, in different codes, the Chinese and English bytes will not be the same, more to calculate the length of the pair, or it will appear garbled

$string = ' Fdsfdsfdsjkww '; Echo substr_replace ($string, Str_repeat (' * ', strlen ($string)-2), 1, strlen ($string)-2);

This is relatively simple.

Substr_replace? To replace a substring of a string

String
The input string.

Replacement
The replacement string.

Start
If start is a positive number, the substitution starts at the start position of the string.

If start is a negative number, the substitution starts at the bottom-starting position of string.

Length
If this parameter is set and is a positive number, the length of the substring that is substituted in the string is represented. If set to negative, it represents the number of characters at the end of the substring to be replaced at the ends of the string. If this parameter is not provided, it defaults to strlen (string). Of course, if length is 0, then the function is to insert replacement into 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.