PHP regular expression processing functions summary of preg_match preg_match_all preg_replace preg_split

  • 2020-05-24 05:18:52
  • OfStack

We've already learned the basic syntax of regular expressions, including delimiters, atoms, metaclashes, and pattern modifiers. In fact, for regular expressions to work, you have to borrow the regular expression handler. In this section, we will introduce the regular expression processing function based on perl in PHP, which mainly includes the processing operations of segmentation, matching, searching, replacing and so on.

Like regular expression 1, regular expression handlers cannot be used independently, and this must be combined to accomplish a specific function. We also said earlier that regular expressions based on perl are faster than regular expression processing functions based on POXIS, so we will only cover regular expressions based on perl starting with preg. Note: do not use regular expressions to process strings when using even string functions, because string processing functions are faster.

Let's take a look at some common regular expression handlers.

1, preg_match() function.

The function preg_match() performs a regular expression match, which is defined as follows:

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

This is essentially a search for the part of subject that matches pattern to be saved in the array matches.
 
<?php 
$pattern = '/<b>.*?<\/b>/'; 
$string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas'; 
if (preg_match($pattern, $string, $arr)) { 
echo " Regular expression <b>{$pattern}</b> And string <b>{$string}</b> The match is successful <br>"; 
print_r($arr); 
} else { 
echo "<font color='red'> Regular expression {$pattern} And string {$string} Matching failure </font>"; 
} 
?> 


2, preg_match_all() function.

The function preg_match_all() performs a global regular expression match whose definition matches that of preg_match(), but matches all the results. See an example:
 
<?php 
$pattern = '/<b>.*?<\/b>/'; 
$string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas'; 
if (preg_match_all($pattern, $string, $arr)) { 
echo " Regular expression <b>{$pattern}</b> And string <b>{$string}</b> The match is successful <br>"; 
print_r($arr); 
} else { 
echo "<font color='red'> Regular expression {$pattern} And string {$string} Matching failure </font>"; 
} 
?> 

The same example as above (with the regular handler function preg_match_all()), but the array of matched results is not the same.

3, preg_replace() function

The function preg_replace() performs a regular expression substitution, which is defined as follows:

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

limit refers to the maximum number of times each mode can be replaced on each subject. The default is -1(unlimited). If count is specified, it will be filled in with the number of completed substitutions.

Note:

A, if subject is an array, preg_replace() returns an array, and otherwise returns a string.

B, if a 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 parameter replacement by using \n or ${n}. (in regular expression patterns we can only use the form \n to get the matched subpatterns, remember!)

D, if you use the schema modifier e, the function can be parsed in the parameter replacement. (in other regular expression handlers, the pattern modifier e is ignored!)

See the following comprehensive example:
 
<?php 
$pattern = '/(php)|(mysql)/e'; 
$string = ' In this string php and mysql It's been replaced with a capital one! '; 
$result = preg_replace($pattern, 'strtoupper("${1}\2")', $string, -1, $count); 
echo $result.'<br>'; 
echo $count; 
?> 

In the example above, we used the schema modifier e so that the strtoupper() function can be parsed as a string. That's what the schema modifier e is for! The parameters ${1} and \2 are submode 1 and submode 2, respectively. The above example replaces the matching submodes php and mysql in the string $string with uppercase letters!

4, preg_split() function.

preg_split executes a regular expression to delimit the string. It is defined as follows:

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

Essentially, you split subject by pattern and return the split array. Where, the maximum number of substrings separated by limit is limit, and the last substring returned will contain all the rest. The value of limit is -1, 0 or null.

Let's look at an example:
 
<?php 
$pattern = '/<p>(.*?)<\/p>/'; 
$string = ' In this string <p>php</p> and <p>mysql</p> Cut up! '; 
$result = preg_split($pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE); 
print_r($result); 
?> 

In the example above, we used the constant PREG_SPLIT_DELIM_CAPTURE to set the return result to include the submode (if set to PREG_SPLIT_NO_EMPTY, preg_split() will return the separated non-empty part). If we remove the parentheses of the positive expression in the example above, php and mysql, the two successfully matched subpatterns, will no longer be included in the result.

We are done with the usual regular expression processing functions. The examples in this section may be a little difficult, but I hope you will try them out carefully. In the later part of regular expression application, we will often use regular expression processing functions.

Related articles: