php preg_filter performs a regular expression search and replace
- 2020-05-12 02:23:03
- OfStack
preg_filter
(PHP 5 > = 5.3.0)
preg_filter - performs 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 only returns (possibly converted) results that match the target. Read the preg_replace() documentation for more details on how this function works.
The return value
If subject is 1 array, return 1 array, and otherwise return 1 string.
If no match is found or an error occurs, an empty array is returned when subject is an array, and NULL is otherwise returned.
sample
Example #1 compares the examples of preg_filter() and preg_replace()
The above routine will output:
PCRE Patterns preg_replace() - performs the search and replace of a regular expression preg_replace_callback() - performs 1 regular expression search and replaces it with 1 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
(PHP 5 > = 5.3.0)
preg_filter - performs 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 only returns (possibly converted) results that match the target. Read the preg_replace() documentation for more details on how this function works.
The return value
If subject is 1 array, return 1 array, and otherwise return 1 string.
If no match is found or an error occurs, an empty array is returned when subject is an array, and NULL is otherwise returned.
sample
Example #1 compares the examples of preg_filter() and preg_replace()
<?php
$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 routine will output:
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 the search and replace of a regular expression preg_replace_callback() - performs 1 regular expression search and replaces it with 1 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