Preg_filter
(PHP 5 >= 5.3.0)
preg_filter-perform a regular expression search and replace
Mixed Preg_filter (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])
Preg_filter () is equivalent to Preg_replace () except that it simply returns (possibly transformed) the result that matches the target. For more detailed information on how this function works, please read the preg_replace () documentation.
return value
If subject is an array, returns an array, and returns a string in other cases.
If no match is found or an error occurs, an empty array is returned when subject is an array, and other conditions return NULL.
Example
Example #1 Compare examples of Preg_filter () and Preg_replace ()
Copy the Code code as follows:
$subject = Array (' 1 ', ' A ', ' 2 ', ' B ', ' 3 ', ' A ', ' B ', ' 4 ');
$pattern = Array ('/\d/', '/[a-z]/', '/[1a]/');
$replace = Array (' a:$0 ', ' b:$0 ', ' c:$0 ');
echo "Preg_filter returns\n";
Print_r (Preg_filter ($pattern, $replace, $subject));
echo "Preg_replace returns\n";
Print_r (Preg_replace ($pattern, $replace, $subject));
?>
The above routines will output:
Copy the Code code as follows:
Preg_filter returns
Array
(
[0] = A:c:1
[1] = B:c:a
[2] = A:2
[3] = B:b
[4] = A:3
[7] = A:4
)
Preg_replace returns
Array
(
[0] = A:c:1
[1] = B:c:a
[2] = A:2
[3] = B:b
[4] = A:3
[5] = A
[6] = B
[7] = A:4
)
- PCRE Patterns
- Preg_replace ()-Performs a search and replace of a regular expression
- Preg_replace_callback ()-Performs a regular expression search and replaces it with a callback
- Preg_grep ()-Returns an array entry for the matching pattern
- Preg_last_error ()-Returns the error code generated by the last Pcre regular execution
The above describes the filter-mapping PHP preg_filter to perform a regular expression search and replace, including the filter-mapping aspect of the content, I hope to be interested in PHP tutorial friends helpful.