Detailed PHP Regular expression substitution implementation _php Tutorial

Source: Internet
Author: User
Tags php regular expression vars
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

 
  
  
  1. Mixed Preg_replace (
  2. Mixed pattern,
  3. Mixed replacement,

Preg_replace: Allows you to replace a regular expression in a string that matches your definition. A simple annotation removal feature:

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

 
 
  1. $string = "April, 2003." ;
  2. $pattern = "/(/w+) (/d+), (/d+)/I" ;
  3. $replacement = "/${1}1,/$3" ;
  4. print preg_replace ($pattern, $replacement, $string );
  5. / * Output
  6. ======
  7. april1,2003
  8. */
  9. ?>

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

 
 
  1. $string =
  2. "The quick brown fox jumped over the lazy dog." ;
  3. $patterns [0] = "/quick/";
  4. $patterns [1] = "/brown/";
  5. $patterns [2] = "/fox/";
  6. $replacements [2] = "Bear";
  7. $replacements [1] = "BLACK";
  8. $replacements [0] = "slow";
  9. print preg_replace ($patterns, $replacements, $string );
  10. / * Output
  11. ======
  12. The bear black slow jumped over the lazy dog.
  13. */
  14. / * by ksorting patterns and replacements,
  15. we should get what we wanted. * *
  16. Ksort ($patterns);
  17. Ksort ($replacements);
  18. print preg_replace ($patterns, $replacements, $string);
  19. / * Output
  20. ======
  21. The slow black jumped over the lazy dog.
  22. */
  23. ?>

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

 
 
    1. !--? php
    2. $patterns = array
    3. ( ,
    4. );
    5. $replace = array
    6. ( "//3///4///1//2" , "$//1 =" );
    7. print preg_replace
    8. ( $patterns /span> , $replace , );
    9. ?>

This example will output:

$startDate = 5/27/1999

Example 4. Using the/E modifier

 
  
  
  1. Preg_replace
  2. ("/( ]*>)/E",
  3. "'//1 '. Strtoupper ('//2 '). ' 3 ' ",
  4. $html _body );
  5. ?>

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

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