PHP Regular Replacement function-----preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]] )

Source: Internet
Author: User
Tags expression engine

preg_replace- Perform a search and replace of a regular expression

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

The subject matching portion of the search pattern to replacement be replaced.

Parameters
pattern

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

You can use some pcre modifiers.

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 pattern and replacement both are arrays, the pattern replacement corresponding elements in each use are replaced. If replacement the element in is pattern less than medium, the extra is pattern replaced with an empty string.

replacementYou can include a back reference \ \n or $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,\\0 and $ A represent 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 replacement use a backslash in, you must use 4 ("\\\\": Because this is the first PHP string, after escaping, is two, and 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 syntax such as \\1 to describe the back reference. For example, \\11 will make preg_replace () do not understand what you want is a \\1 back reference immediately following a source 1, or a \\11 The back reference is not followed by anything. In this case the solution is to use ${1}1. This creates a separate, forward -referencing, 1-independent source.

When the deprecated 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 (\ ' $1\ ') +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 it is an array, the search and replace is subject performed on each element, and the return value is an array.

limit

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

count

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

return value?

If subject it is an array, preg_replace () returns an array, and in other cases returns a string.

If the match is found, the replacement subject is returned, and in other cases the return is unchanged subject . If an error occurs, return NULL .

Example #1 Use the back reference to follow the value text

<?php
$string = ‘April 15, 2003‘;
$pattern = ‘/(\w+) (\d+), (\d+)/i‘;
$replacement = ‘${1}1,$3‘;
echo preg_replace($pattern, $replacement, $string);
?>

The above routines will output:

april1,2003
"<p><br></p>"
  

Preg_replace ('/(/i ', "\${1} \${3}\${6}.\${7}\${8}>", $string);

"<p><br></p> "

PHP Regular Replacement function-----preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]] )

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.