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, when When subject is an array, an empty array is returned, otherwise NULL is returned.
Example
Example #1 Example of comparing preg_filter() and preg_replace()
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));
?>
The above routine will output:
Copy code The code is 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 regular expression search and replace
-
preg_replace_callback() - performs a regular expression search and replaces with a callback
-
preg_grep() - Returns array entries matching a pattern
-
preg_last_error() - Returns the error code generated by the last PCRE regular execution
http://www.bkjia.com/PHPjc/325093.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325093.htmlTechArticlepreg_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 [,...