PHP regular expression replacement implementation _ PHP Tutorial

Source: Internet
Author: User
PHP regular expression replacement implementation. What is the implementation of PHP regular expression replacement? First, I will introduce you to PHPpreg_replace. PHPpreg_replace is our implementation method. how can we replace PHP regular expressions with PHP regular expressions? First, we will introduce you to PHP preg_replace. the use of PHP preg_replace is our implementation method, so we will start with the instance for the PHP regular expression replacement implementation process.

Related concepts of PHP regular expression replacement:

Preg_replace: Search and replace regular expressions.

 
 
  1. mixed preg_replace (
  2. mixed pattern,
  3. mixed replacement,
  4. mixed subject [, int limit])

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

 
 
  1. preg_replace('[(/*)+.+(*/)]', '', $val);

This code can remove multiple comments using/* comments */format in PHP and CSS. The three parameters are the regular expression, the string to be replaced, and the target string to be replaced (here the removal function is required, so it is a blank string-> ''). If you want to match sub-rules, you can use $0 to represent all matches, $1, $2, and so on.

Search for matches in pattern in subject and replace them with replacement. If limit is specified, only limit matches are replaced. if limit is omitted or its value is-1, all matches are replaced.

Replacement can contain reverse references in the form of // n or (starting from PHP 4.0.4) $ n. The latter is preferred. Each such reference will be replaced with the text that matches the child pattern in the nth captured parentheses. N can range from 0 to 99. // 0 or $0 indicates the text that is matched by the entire mode. Count left parentheses from left to right (starting from 1) to obtain the number of child modes.

When the replacement mode is followed by a reverse reference followed by a number (that is, the number next to a matching mode), the familiar // 1 symbol cannot be used to represent the reverse reference. For example,/11 will make preg_replace () confused about whether a reverse reference of // 1 is followed by a number 1 or a reverse reference of // 11. The solution in this example is to use/$ {1} 1. This will form an isolated $1 reverse reference, and make the other 1 just plain text.

Example of PHP regular expression replacement:

Example 1. reverse reference followed by a number

 
 
  1. $string = "April 15, 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 numerical order of the index. If indexes are used to identify which pattern will be replaced by which replacement, ksort () should be used to sort the array before preg_replace () is called.

Example 2. use the index 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 bear jumped over the lazy dog.
  22. */
  23. ?>

If the subject is an array, it searches and replaces each item in the subject and returns an array.

If both pattern and replacement are arrays, preg_replace () extracts values from them to search and replace subject. If the value in replacement is less than that 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 makes no sense.

The/e modifier enables preg_replace () to treat the replacement parameter as PHP code (after appropriate reverse references are replaced ). Tip: Make sure that the replacement constitutes a valid PHP code string. otherwise, PHP will report a syntax parsing error in the row containing preg_replace.

Example 3. replace several values

 
 
  1. $patterns = array
  2. ("/(19|20)(/d{2})-(/d{1,2})-(/d{1,2})/",
  3. "/^/s*{(/w+)}/s*=/");
  4. $replace = array
  5. ("//3///4///1//2", "$//1 =");
  6. print preg_replace
  7. ($patterns, $replace, "{startDate} = 1999-5-27");
  8. ?>

This example will output:

$ StartDate = 5/27/1999

Example 4. use 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 in uppercase, and the above instances support PHP 3> = 3.0.9, PHP 4.

The PHP regular expression replacement related content will be introduced here, I hope that will help you understand and master PHP regular expression replacement.


Why? First, we will introduce you to PHP preg_replace. the use of PHP preg_replace is our implementation method, so the PHP regular expression replacement implementation...

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.