PHP array-one-to-single substitution implementation code the word between two positions is replaced by the * number

Source: Internet
Author: User
PHP Substr_replace will specify the characters between two positions to replace the code with the * number, the required friends can refer to below.

The code is as follows:

$username = "Zongzi"; Echo substr_replace ($username, ' * * ', ' 1 ', ' 2 ');

Definition and usage

The Substr_replace () function replaces part of a string with another string.

Grammar

Substr_replace (String,replacement,start,length)
Parameters Description
String Necessary. Specifies the string to check.
Replacement Necessary. Specifies the string to insert.
Start

Necessary. Specifies where the string begins to be replaced.

  • Positive number-starts with the first offset

  • Negative number-replaces the start offset starting at the end of a string

  • 0-Start the substitution at the first character in the string

Charlist

Optional. Specifies the number of characters to replace.

  • Positive number-The length of the string being replaced

  • Negative-the number of characters to be replaced starting at the end of the string

  • 0-Insert rather than replace

Hints and Notes

Note: If start is negative and length is less than or equal to start, length is 0.
Example

The code is as follows:

<?php Echo substr_replace ("Hello World", "Earth", 6);?>

Output:
Hello Earth

The following methods can be implemented to match the key words and special processing functions, the code is as follows

<?php header ("content-type:text/html; Charset=utf-8 "); function Multiple_replace_words ($word, $replace, $string, $tmp _match= ' #a_a # ') {Preg_match_all ('/') $word. ' /', $string, $matches); Match all keywords $search = explode (', ', '/'. Implode ('/,/', $matches [0]). ' /'); There is no matching keyword if (empty ($matches [0])) return false; Special substitution Settings $count = count ($matches [0]); foreach ($replace as $key + $val) {if (!isset ($matches [0][$key]) unset ($replace [$key]);//Reject out-of-bounds substitution}//merge special substitution array with matching array for ($i =0; $i < $count; $i + +) {$matches [0][$i] = isset ($replace [$i])? $replace [$i]: $matches [0][$i];} $replace = $ Matches[0]; Prevents the replacement loop, that is, the replacement character is still the substituted character, at which time it temporarily replaces a specific character $tmp_match $replace = implode (', ', $replace); $replace = Str_replace ($word, $tmp _match, $replace); Temporary substitution of matching characters $replace = explode (', ', $replace); Replacement processing $string = Preg_replace ($search, $replace, $string, 1); Replaces only one $string in the array at a time = Str_replace ($tmp _match, $word, $string); Restores the temporary replacement of the matching character to return $string; }//Example 1 $string = ' aaabaaacaaadaaa '; $word = ' AAA '; $replace = Array (null, ' xxx ', ' yyy '); ECho ' original: '. $string. ' <br/> output: '. Multiple_replace_words ($word, $replace, $string). ' <br/><br/> '; Example 2 $string = ' Chinese aaab Chinese ccaaad Chinese eee '; $word = ' Chinese '; $replace = Array (null, ' (replace Chinese 2) ', ' (replace Chinese 3) '); Echo ' original: '. $string. ' <br/> output: '. Multiple_replace_words ($word, $replace, $string); /* Output: Original: Aaabaaacaaadaaa output: aaabxxxcyyydaaa Original: Chinese aaab Chinese ccaaad Chinese eee output: Chinese Aaab (replace Chinese 2) Ccaaad (replace Chinese 3) Eee//*/

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.