The preg_replace () parameter is an example of multiple replacement of arrays.

Source: Internet
Author: User

This article introduces some solutions to self-understanding and replacement times when preg_replace () parameters are replaced multiple times in arrays.

Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])

Pattern
The pattern to be searched. A string or string array can be used.

You can use some PCRE modifiers, including 'E' (PREG_REPLACE_EVAL), which can be specified for this function.

Replacement
String or string array to be replaced. if this parameter is a string and pattern is an array, all the modes use this string to replace. if both pattern and replacement are arrays, each pattern is replaced by the corresponding elements in replacement. if replacement contains fewer elements than pattern, the extra pattern is replaced with an empty string.

Replacement can contain back-to-reference n or (php 4.0.4 or above) $ n. The latter is preferred in syntax. each such reference will replace the text captured by the nth capturing sub-group that is matched. n can be 0-99, and $0 represents the complete pattern matching text. the serial number counting method of the captured sub-group is as follows: left parentheses of the captured Sub-Group start from left to right and start from 1. if you want to use a backslash in replacement, you must use four ("", note: because this is the first php string, after escaping, it is two, after the Regular Expression Engine is used, it is considered as a backslash of the original article ).

When it works in the replacement mode and the back reference is followed by another number (for example, adding an original number after a matching mode ), you cannot use a syntax like 1 to describe backward references. for example, 11 will make preg_replace () unable to understand that what you want is a 1 Back-to-Back Reference followed by an original article 1, or a 11 back-to-reference followed by nothing. in this case, the solution is to use $ {1} 1. this creates an independent $1 Back Reference, an independent original 1.

When the e modifier is used, this function will escape some characters (I .e., ', ", and NULL) and then carry out backward reference replacement. after completing these steps, make sure that there are no syntax errors caused by single or double quotation marks (for example, 'strlen ('$ 1') + strlen ("$2") after parsing the back reference ") '). make sure that the PHP string syntax and eval syntax are met. after replacement, the engine evaluates the result string as the php code using the eval method and uses the returned value as the final string to be replaced.

Subject
String or string array to be searched and replaced.

If the subject is an array, search and replace it with each element of the subject, and the return value is also an array.

Limit
The maximum number of times each mode is replaced on each subject. The default value is-1 (unlimited ).

Count
If specified, it will be filled with the number of replacement completed.


Yes-use a function to execute a regular expression for searching and replacing. 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: Copy code

$ 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 returnsn

"; 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.

The above 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

Use each pattern in $ pattern to match every element in $ subject in sequence. If the pattern matches, replace the $ replace corresponding to $ pattern. In the above example, replace the pattern more than once.

Control the number of times preg_replace is replaced

The Code is as follows: Copy code

$ V = array ('vv ', 'bb'); $ str = "vv, cc, abcde, www.bkjia.com, vv "; $ str = preg_replace ("/$ v [0]/", "". $ v [0]. "", $ str, 1); echo $ str; // bb, cc, abcde, www.bkjia.com, vv

$ Str = preg_replace ("/$ v [0]/", "". $ v [0]. "", $ str, 2); // bb, cc, abcde, www.bkjia.com, bb


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.