Preg_replace () parameter is an example of an array of multiple substitutions _php tutorial

Source: Internet
Author: User
Tags expression engine
This article to the students about the Preg_replace () parameters are several multiple substitution of some self-understanding and replacement number of solutions, there is a need to understand the same non-reference.

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

Pattern
The mode to search for. Can make a string or array of strings.

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

Replacement
A string or array of strings to replace. If this argument is a string and pattern is an array, then all patterns are replaced with this string. If both pattern and replacement are arrays, each pattern is replaced with the corresponding element in replacement. If the elements in the replacement are less than the pattern, the extra pattern is replaced with an empty string.

The replacement can contain a back reference N or (PHP 4.0.4 or more) $n, which is syntactically preferred. Each such reference will be matched to the text that is captured by the nth capturing subgroup to replace. n can be 0-99, and $ A represents the complete pattern matching text. The ordinal count of the capturing subgroup is: The opening parenthesis representing the capturing subgroup is left to right, starting at 1. If you want to use backslashes in replacement, you must use 4 ("": Because this is the first PHP string, after escaping, it is two, then after the regular expression engine is considered a text backslash).

When working in replacement mode and the back reference is followed by a second number (for example, adding a text number immediately after a matching pattern), you cannot use the syntax of 1 to describe the back reference. For example, 11 will make preg_replace () do not understand what you want is a 1 back reference followed by a source 1, or a 11 back reference followed by nothing. In this case the solution is to use ${1}1. This creates a separate, forward-referencing, 1-independent source.

When the E modifier is used, the function escapes some characters (that is, ', ', and null) and then replaces it with a back reference. When these are done, make sure that there are no single or double quotation marks due to syntax errors after parsing the back reference (e.g. ' strlen (' $ ') ' +strlen ("$"). Ensure that PHP is compliant with the string syntax and that it conforms to the eval syntax. Because after the substitution is complete, the engine evaluates the resulting string as PHP code using the Eval method and returns the return value as a string that will be substituted for the final participation.

Subject
A string or array of strings to search for and replace.

If subject is an array, the search and replace is performed on each element of the subject, and the return value is an array.

Limit
The maximum number of times each pattern is replaced on each subject. The default is-1 (infinity).

Count
If specified, it will be populated with the number of replacements completed.


Yes-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.


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

Streamline process

1->a:1->a:c:1

A->b:a->b:c:a

2->a:2

B->b:b

A (not replaced if no match is met)

B (IBID.)

4->a:4

Take $pattern each pattern in turn to match each element in the $subject, matching to the $pattern corresponding to the $replace swap, as above, may be more than one replacement

Control the number of preg_replace replacements

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


http://www.bkjia.com/PHPjc/445282.html www.bkjia.com true http://www.bkjia.com/PHPjc/445282.html techarticle This article to the students about the Preg_replace () parameters are several multiple substitution of some self-understanding and replacement number of solutions, there is a need to understand the same non-reference. Mixed Preg ...

  • Related Article

    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.