Home  >  Article  >  Backend Development  >  filter-mapping php preg_filter performs a regular expression search and replace

filter-mapping php preg_filter performs a regular expression search and replace

WBOY
WBOYOriginal
2016-07-29 08:48:08882browse

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

[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 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


  • The above introduces filter-mapping php preg_filter to perform a regular expression search and replacement, including filter-mapping content. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn