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 only returns (possibly transformed) results that match the target. For more details on how this function works, please read the preg_replace() documentation.
Return Value
If subject is an array, returns an array, otherwise returns a string.
If no match is found or an error occurs, an empty array is returned when subject is an array, and NULL is returned in other cases.
Example
Example #1 Compare preg_filter( ) and preg_replace() example
Copy code The code is 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 returnsn";
print_r(preg_filter($pattern, $replace, $subject));
echo "preg_replace returnsn";
print_r (preg_replace($pattern, $replace, $subject));
?> ] => A:C:1
[1] => B:C:a
[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 regular expression search and replace
preg_replace_callback() - Performs a regular expression search and replaces using a callback
preg_grep() - Returns array entries matching the pattern
preg_last_error() - Returns the error code generated by the last PCRE regular execution