Regular expression-based regular expression processing function _ PHP Tutorial

Source: Internet
Author: User
Regular expression: a regular expression processing function. We have learned the basic syntax of regular expressions, including delimiters, atoms, metacharacters, and pattern modifiers. In fact, to work with a regular expression, you must use the basic syntax that we have learned before, including the delimiters, atoms, metacharacters, and pattern modifiers. In fact, to work with a regular expression, you must use a regular expression to process the function. This section describes the perl-based regular expression processing functions in PHP, including division, matching, search, replacement, and other processing operations, let's get started.

Like regular expressions, regular expression processing functions cannot be used independently, but must be combined to complete specific functions. As we have mentioned earlier, perl-based regular expressions are faster than POXIS regular expressions to process functions. Therefore, we only introduce perl-based regular expressions starting with preg.Note: When string functions can be used for processing, do not use regular expressions to process strings because string processing functions are faster.

The following describes some common regular expression processing functions.

1, preg_match () function.

The preg_match () function executes a regular expression matching, which is defined as follows:

 
 
  1. int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

In fact, it is to search the part of the subject that matches pattern to save it in the array matches. See the example below:

 
 
  1. $ Pattern = '/.*?/';
  2. $ String ='Welcome To PhpfunsDsadsadas ';
  3. If (preg_match ($ pattern, $ string, $ arr )){
  4. Echo "regular expression{$ Pattern}And string{$ String}Matched
    ";
  5. Print_r ($ arr );
  6. } Else {
  7. Echo "the regular expression {$ pattern} and string {$ string} failed to match ";
  8. }
  9. ?>
2, preg_match_all () function.

The preg_match_all () function executes a Global Regular Expression Matching. its definition is the same as that of the preg_match () function, except that all results are matched. See the example:

 
 
  1. $ Pattern = '/.*?/';
  2. $ String ='Welcome To PhpfunsDsadsadas ';
  3. If (preg_match_all ($ pattern, $ string, $ arr )){
  4. Echo "regular expression{$ Pattern}And string{$ String}Matched
    ";
  5. Print_r ($ arr );
  6. } Else {
  7. Echo "the regular expression {$ pattern} and string {$ string} failed to match ";
  8. }
  9. ?>

The above example is still used (only the regular expression handler is replaced with preg_match_all (), but the matching result array content is different.

3, preg_replace () function

The preg_replace () function executes a regular expression replacement, which is defined as follows:

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

In fact, it is to search for the part in the subject that matches pattern and replace it with replacement. here, limit refers to the maximum number of times each mode is replaced on each subject. the default value is-1 (unlimited ). if count is specified, it is filled with the number of completed replicas.

Note:

A. If the subject is an array, preg_replace () returns an array, and otherwise returns A string.

B. If the match is found, the replaced subject is returned. Otherwise, the unchanged subject is returned. If an error occurs, NULL is returned.

C. The submode can be applied to the replacement parameter. the usage is n or $ {n }.(In the regular expression mode, we can only use the n form to obtain the matched sub-pattern. remember !)

D. If the pattern modifier e is used, the function can be parsed in The replacement parameter. (In other regular expression processing functions, the pattern modifier e is ignored !)

See the following comprehensive example:

 
 
  1. $ Pattern = '/(php) | (mysql)/E ';
  2. $ String = 'The php and mysql strings are replaced with uppercase ones! ';
  3. $ Result = preg_replace ($ pattern, 'strtoupper ("$ {1} 2") ', $ string,-1, $ count );
  4. Echo $ result .'
    ';
  5. Echo $ count;
  6. ?>

In the above example, we use the pattern modifier e, so that the strtoupper () function can be parsed as a string, which is the role of pattern modifier e! The $ {1} and 2 parameters are sub-Mode 1 and sub-Mode 2, respectively. The function of the above example is to replace the Child pattern matching in the string $ string with a uppercase letter for php and mysql!

4. preg_split () function.

Preg_split execute a regular expression to separate strings. It is defined as follows:

Array preg_split (string $ pattern, string $ subject [, int $ limit =-1 [, int $ flags = 0])

Actually, subject is divided by pattern, and the split array is returned. Here, limit will limit that only the limit substrings can be separated, and the last returned substring will contain all the remaining parts. when the limit value is-1, 0 or null, it indicates "unlimited ".

Let's look at an example:

 
 
  1. $ Pattern = '/

    (.*?)

    /';
  2. $ String = 'in the string

    Php

    And

    Mysql

    Split! ';
  3. $ Result = preg_split ($ pattern, $ string,-1, PREG_SPLIT_DELIM_CAPTURE );
  4. Print_r ($ result );
  5. ?>

In the above example, we use the constant PREG_SPLIT_DELIM_CAPTURE to set the child mode in the returned result (if it is set to PREG_SPLIT_NO_EMPTY, preg_split (), the non-empty part after the return is separated .) If we remove the parentheses of the regular expression in the above example, the results will no longer contain the successful child modes of php and mysql.

This section describes the common regular expression processing functions, and the examples in this section may be difficult. However, I hope you can experiment with them carefully and understand the application section of the regular expression below, we often use regular expressions to process functions.

Original article address:

Escape character. In fact, to make a regular expression work, you must borrow it...

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.