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. we have learned the basic syntax of regular expressions, 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:
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:
The code is as follows:
$ Pattern = '/.*? <\/B> /';
$ String ='Welcome To PhpfunsDsadsadas ';
If (preg_match ($ pattern, $ string, $ arr )){
Echo "regular expression{$ Pattern}And string{$ String}Matched
";
Print_r ($ arr );
} Else {
Echo "the regular expression {$ pattern} and string {$ string} failed to match ";
}
?>
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:
The code is as follows:
$ Pattern = '/.*? <\/B> /';
$ String ='Welcome To PhpfunsDsadsadas ';
If (preg_match_all ($ pattern, $ string, $ arr )){
Echo "regular expression{$ Pattern}And string{$ String}Matched
";
Print_r ($ arr );
} Else {
Echo "the regular expression {$ pattern} and string {$ string} failed to match ";
}
?>
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 sub-mode can be applied to the replacement parameter. the usage is $ {n }. (In the regular expression mode, we can only use the \ n form to obtain the matched child mode. 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:
The code is as follows:
$ Pattern = '/(php) | (mysql)/E ';
$ String = 'The php and mysql strings are replaced with uppercase ones! ';
$ Result = preg_replace ($ pattern, 'strtoupper ("$ {1} \ 2") ', $ string,-1, $ count );
Echo $ result .'
';
Echo $ count;
?>
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:
The code is as follows:
$ Pattern = '/
(.*?) <\/P> /';
$ String = 'in the string
Php
And
Mysql
Split! ';
$ Result = preg_split ($ pattern, $ string,-1, PREG_SPLIT_DELIM_CAPTURE );
Print_r ($ result );
?>
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.