Some details of the Preg_replace

Source: Internet
Author: User
Tags smarty template

The. $pattern is an array , $replace is also an array , then the corresponding element is replaced

Php

Preg_replace has five parameters , Three of which are required parameters

Preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int $count]]);

$pattern can be a string , an array of strings, or preg

$replace is a string or array of strings for substitution

$subject target string or array of target strings

$limit the maximum number of substitutions per pattern on each $subject , the default is -1 ( Unlimited )

$count returns the number of times the target string has been replaced

There are several points to note in Repalce and pattern.

1. $pattern is an array , $replace is also a string , then all patterns use this string to match ;

2. $pattern is an array , $replace is also an array , then the corresponding element is replaced

3. If the array $pattern is greater than $replace, the extra $pattern is replaced with an empty string,


$replace less than $patterns, the matched values are all replaced with an empty string

4. For the above example, the${1},${2},${3},this is called including a back reference .,the number inside represents the number of bits the atom appears in .,There are two ways of writing,one is\\n,one is${n},The second way to do it is recommended.Will match the target string to thepatternthe atoms in the,change the format of the target string as required,or the matched atoms are output as-is (two examples here)

Divide the time by the underscore, ${3} is the third atom ((\d{1,2}) that matches to $patterns, and ${4} is the fourth atom (($patterns}) that matches to \d{1,2, and so on,

For example, familiar with the Smarty template engine in the post-compilation replacement method

5. Use of the $limit of The fourth parameter , The fourth parameter means the maximum number of times each pattern is replaced on each subject


If limit is n, it replaces only n times with regular

6. The use of the fifth parameter $count, $count means, subject the number of times the target string has been replaced, take the 5th example



The example in the above code runs the following results:

Additional:

What is the relationship between Preg_replace and Str_replace?

We can understand that Str_replace is a subset of Preg_repalce. Only the first parameter in Str_replace is written in string, and Preg_replace is written as a regular expression

Some details of the Preg_replace

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.