Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])
is to perform a regular expression of the search and replace the function, usually we use it when the end of a replacement, and today see the PHP manual found a more difficult to understand (self-think) of the example to share to everyone.
<?php $subject = Array (' 1 ', ' A ', ' 2 ', ' B ', ' 3 ', ' A ', ' B ', ' 4 '); $pattern = Array ('/\d/', '/[a-z]/', '/[1a]/'); $replace = Array (' a:$0 ', ' b:$0 ', ' c:$0 '); echo "Preg_replace returns\n<pre/>"; Print_r (Preg_replace ($pattern, $replace, $subject))?>
The results are as follows:
At first glance, I'm dizzy.
In general, if both the match pattern and the replacement content are arrays, they should correspond if the elements in the replacement are less than the pattern, and the extra pattern is replaced with an empty string.
$pattern is like a scanner that sweeps to match the corresponding $replace replacement
For the previous example, the replacement process is as follows:
/\d/Scan the $subject in 1, match, match content is $ (that is, 1) to replace 1 with a:1
Then use/[a-z]/scan a:1 mismatch, do not replace, continue to use [1a] scanning a:1, matching content of 1 (that is, $), the A:1 1 for C:1
The first item was eventually replaced with the 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 (does not replace if no match is met)
B (ibid.)
4->a : 4
Summing up a point, take $pattern each of the patterns in turn to match each element in the $subject, matching to the $pattern corresponding to the $replace swap, as in the example above, may be replaced more than once