preg_filter ($pattern, $replacement, $subject, $limit, $count)Parameters: This function takes the five parameters mentioned above and described below.
- $pattern:this parameter contains the string element to search for, which can be a string or an array of strings.
- $replace:Required parameter that specifies a string or array with strings to replace.
- $subject:string or array with strings to find and replace.
- $limit:This parameter specifies the maximum possible replacements for each pattern.
- $count: this is an optional parameter that is used to fill with the number of replacements performed.
Program 1:
Php
// PHP illustration program
// preg_filter function
$string
=
’November 01, 2018’
;
$pattern
=
’/ (w + ) (d +), (d +) / i’
;
$replacement
=
’${1 } 02, $3’
;
// print the result
echo
preg_filter (
$pattern
,
$replacement
,
$string
);
?>
Exit:November 02, 2018Program 2:
php
// PHP program to illustrate the preg_filter function
$subject
=
array
(
’1’
,
’ GFG’
,
’2’
,
’Geeks’
,
’ 3’
,
’GCET’
,
’ Contribute’
,
’4’
);
$pattern
=
array
(
’/ d /’
,
’ / [az] / ’
,
’ / [1a] / ’
);
$replace
=
array
(
’X: $0’
,
’ Y: $0’
,
’Z: $0’
) ;
echo
" Returned Array by preg_filter "
;
print_r (preg_filter (
$pattern
,
$replace
,
$subject
));
?>
Exit:Returned Array by preg_filterArray ([0] = > X: Z: 1 [2] = > X: 2 [3] = > GY: eY: eY: kY: s [4] = > X: 3 [6] = > CY: oY: nY: tY: rY: iY: bY: uY: tY: e [7] = > X: 4)Link: http://php.net/manual/en/function.preg-filter.php