This article introduces to students some solutions to self-understanding and replacement times when the preg_replace() parameters are array replacements multiple times. If you need to know more, please do not refer to it.
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
pattern
The pattern to search for. Can be a string or an array of strings.
A number of PCRE modifiers, including 'e' (PREG_REPLACE_EVAL), can be specified for this function.
replacement
The string or array of strings used for replacement. If this parameter is a string, and pattern is an array, then all patterns use this string for replacement. If pattern and replacement are both arrays, each pattern uses replacement Replace the corresponding elements in replacement. If there are fewer elements in replacement than in pattern, the extra pattern will be replaced with an empty string.
Replacement can contain backreferences n or (available from PHP 4.0.4 and above) $n, the latter is syntactically preferred. Each such reference will be replaced by the text captured by the matching nth capture subgroup . n can be 0-99, � and $0 represent the complete pattern matching text. The serial number counting method of the capturing subgroup is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use it in replacement Four backslashes must be used ("", annotation: because this is first a PHP string, after escaping, there are two, and then it is considered to be one original backslash after being passed through the regular expression engine).
When working in replacement mode and the backreference needs to be followed by another number (for example: adding a source number immediately after a match pattern), you cannot use the syntax 1 to describe the backreference. For example, 11 will cause preg_replace() to not understand whether you want a 1 backreference followed by an original 1, or an 11 backreference followed by nothing. The solution in this case is to use ${1} 1. This creates an independent $1 backreference, an independent original text 1.
When using the e modifier, this function will escape some characters (i.e.:', ", and NULL) and then perform backreference replacement. When this is done, please make sure that there are no single quotes or single quotes after the backreference is resolved. Syntax errors caused by double quotes (for example: 'strlen('$1')+strlen("$2")'). Make sure to comply with PHP's string syntax and comply with eval syntax. Because after completing the replacement, the engine will replace the resulting characters The string is evaluated as a php code using the eval method and the return value is used as the string that ultimately participates in the replacement.
subject
The string or array of strings to search and replace.
If the subject is an array, search and replacement will be performed on each element of the subject, and the return value will also be an array.
limit
The maximum number of substitutions per subject per pattern. Default is -1 (unlimited).
count
If specified, will be filled with the number of completed substitutions.
Yes - a function that performs a regular expression search and replace. Usually when we use it, it usually ends with one substitution. Today I looked at the PHP manual and found a relatively difficult to understand (I think) example to share with you.
The code is as follows | Copy code | ||||
|
The results are as follows:
At first glance, I was dizzy
Generally, if the matching pattern and replacement content are both arrays, they should correspond. If there are fewer elements in replacement than in pattern, the extra pattern will be replaced with an empty string.
$pattern is like a scanner. If it scans a match, it will be replaced with the corresponding $replace
The replacement process for the above example is as follows
/d/Scan 1 in $subject and it matches. If the matching content is $0 (that is, 1), replace 1 with A:1
Then use /[a-z]/ to scan A:1 if it does not match, and do not replace it. Continue to use [1a] to scan A:1. If the matching content is 1 (that is, $0), replace the 1 in A:1 with C:1
The first item was eventually replaced with A:C:1
Simplify the process
1->A:1->A:C:1
a->B:a->B:C:a
2->A:2
b->B:b
A (If there is no match, it will not be replaced)
B(same as above)
4->A:4
Take each pattern in $pattern to match each element in $subject in turn. When matched, replace it with the $replace corresponding to $pattern. As in the above example, it may be replaced more than once
Control preg_replace replacement times
The code is as follows | Copy code | ||||
$str=preg_replace("/$v[0]/","".$v[0]."",$str,2); //bb,cc,abcde,www.bkjia.com, bb
|