What is the implementation of the PHP regular expression substitution? First introduce you to the PHP preg_replace,php Preg_replace use is our implementation of the method, then for the PHP regular expression substitution implementation process we start from the instance.
Related concepts of PHP regular expression substitution:
Preg_replace: Perform search and replace of regular expressions
- Mixed Preg_replace (
- Mixed pattern,
- Mixed replacement,
Preg_replace: Allows you to replace a regular expression in a string that matches your definition. A simple annotation removal feature:
- preg_replace (' [(/*) +.+ (*/)]'$val
This code can remove the multiline comment in PHP and CSS using the/* Comment */format. Three of these parameters are regular expressions, the string to replace and the target string to replace (here to remove the function, so it is a blank string--"). If you want to match the secondary rules, you can use $ A to represent all matches, $, $, etc., and so on, representing the respective secondary rules.
Searches the subject for a match in pattern mode and replaces it with replacement. If limit is specified, only the limit match is replaced, and if the limit is omitted or the value is-1, all occurrences are replaced.
Replacement can contain a//n form or a reverse reference (since PHP 4.0.4) $n form, preferably using the latter. Each of these references will be replaced with text that matches the sub-pattern in the nth captured parentheses. n can be from 0 to 99, where//0 or $ refers to text that is matched by the entire pattern. The left parenthesis is counted from left to right (starting at 1) to get the number of sub-patterns.
When the replacement pattern is followed by a number immediately after a reverse reference (that is, a number immediately following a matching pattern), you cannot use a familiar//1 symbol to represent a reverse reference. //11, for example, will make preg_replace () unclear if it wants a//1 inverse reference followed by a number 1 or a//11 inverse reference. The workaround in this example is to use/${1}1. This creates an isolated reverse reference, and makes the other 1 simple text.
Related examples of PHP regular expression substitution:
Example 1. Inverse reference followed by the use of numbers
-
- $string = "April, 2003." ;
- $pattern = "/(/w+) (/d+), (/d+)/I" ;
- $replacement = "/${1}1,/$3" ;
- print preg_replace ($pattern, $replacement, $string );
- / * Output
- ======
- april1,2003
- */
- ?>
If a match is found, the replaced subject is returned, otherwise the original unchanged subject is returned.
Each parameter (except limit) of Preg_replace () can be an array. If both pattern and replacement are arrays, they are processed in the order in which their key names appear in the array. This is not necessarily the same as the numeric order of the index. If you use an index to identify which pattern will be replaced by which replacement, you should sort the array with Ksort () before calling Preg_replace ().
Example 2. Using an indexed array in Preg_replace ()
-
- $string =
- "The quick brown fox jumped over the lazy dog." ;
- $patterns [0] = "/quick/";
- $patterns [1] = "/brown/";
- $patterns [2] = "/fox/";
- $replacements [2] = "Bear";
- $replacements [1] = "BLACK";
- $replacements [0] = "slow";
- print preg_replace ($patterns, $replacements, $string );
- / * Output
- ======
- The bear black slow jumped over the lazy dog.
- */
- / * by ksorting patterns and replacements,
- we should get what we wanted. * *
- Ksort ($patterns);
- Ksort ($replacements);
- print preg_replace ($patterns, $replacements, $string);
- / * Output
- ======
- The slow black jumped over the lazy dog.
- */
- ?>
If subject is an array, the search and replace is performed on each item in the subject, and a collection is returned.
If both pattern and replacement are arrays, then preg_replace () extracts the values from each to search for and replace the subject. If the value in replacement is less than in pattern, an empty string is used as the remaining replacement value. If pattern is an array and replacement is a string, this string is used as the replacement value for each value in pattern. In turn, it doesn't make sense.
The/E modifier causes preg_replace () to treat the replacement parameter as PHP code (after the appropriate reverse reference is replaced). Tip: To make sure that replacement constitutes a valid PHP code string, PHP will have a parsing error in the report line that contains Preg_replace ().
Example 3. Replace a number of values
- !--? php
- $patterns = array
- ( ,
- );
- $replace = array
- ( "//3///4///1//2" , "$//1 =" );
- print preg_replace
- ( $patterns /span> , $replace , );
- ?>
This example will output:
$startDate = 5/27/1999
Example 4. Using the/E modifier
-
- Preg_replace
- ("/( ]*>)/E",
- "'//1 '. Strtoupper ('//2 '). ' 3 ' ",
- $html _body );
- ?>
This will make all HTML tags in the input string uppercase, with the above instance running the supported version of PHP 3>= 3.0.9, PHP 4.
The relevant content of the PHP regular expression substitution is introduced to you here, hopefully that will help you understand and master the PHP regular expression substitution.
http://www.bkjia.com/PHPjc/446578.html www.bkjia.com true http://www.bkjia.com/PHPjc/446578.html techarticle What is the implementation of the PHP regular expression substitution? First introduce you to the PHP preg_replace,php Preg_replace use is our implementation of the method, then for the PHP regular expression substitution implementation ...