Explain how to replace phppreg_replace with an instance. The copy code is as follows: mixedpreg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit-1 [, intlt ;? Php $ subjectarray (1, a, 2, B, 3,
The code is as follows:
Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])
It is a function used to execute a regular expression to search for and replace the regular expression. Usually we end with a replacement. today, I read the php manual and find it hard to understand (I think) to share with you.
The code is as follows:
$ 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
";
Print_r (preg_replace ($ pattern, $ replace, $ subject ));
?>
The result is as follows:
At first glance, I was dizzy.
Generally, if both the matching mode and replacement content are arrays, they should correspond. if the elements in replacement are less than those in pattern, the extra pattern should be replaced with an empty string.
$ Pattern is like a scanner. scanning the matching pattern is used to replace the corresponding $ replace.
For the above example, the replacement process is as follows:
/\ D/scan for 1 in $ subject. if the matching content is $0 (that is, 1), replace 1 with A: 1.
Then, use/[a-z]/to scan for A: 1 that does not match, instead, use [1a] to scan for A: 1. the matching content is 1 (that is, $0 ), replace 1 in A: 1 with C: 1.
The first item is eventually replaced with A: C: 1
Simplified process:
1-> A: 1-> A: C: 1
A-> B: C:
2-> A: 2
B-> B: B
A (if there is no matching, it will not be replaced)
B (same as above)
4-> A: 4
To sum up, use each pattern in $ pattern to match every element in $ subject in sequence. if it matches, replace the $ replace corresponding to $ pattern, as shown in the preceding example, may be replaced more than once
The http://www.bkjia.com/PHPjc/825007.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825007.htmlTechArticle code is as follows: mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int lt ;? Php $ subject = array ('1', 'A', '2', 'B', '3', 'A '...