preg_grep
Return array entries that match the pattern
Syntax
preg_grep ( string $pattern , array $input [, int $flags = 0 ] ) : array
Parameters
pattern
The pattern to search for, as a string.
input
The input array.
flags
If set to PREG_GREP_INVERT, this function returns the elements of the input array that do not match the given pattern.
Return
Returns an array indexed using the keys from the input array.
Examples
pattern input
<? $pattern = "#^(\d+)?\.\d+$#"; $input = array(0, 1, 2.2, 3.3); $return = preg_grep($pattern, $input); print_r($return); ?>
Array ( [2] => 2.2 [3] => 3.3 )
flags
<? $pattern = "#^(\d+)?\.\d+$#"; $input = array(0, 1, 2.2, 3.3); $flags = PREG_GREP_INVERT; $return = preg_grep($pattern, $input, $flags); print_r($return); ?>
Array ( [0] => 0 [1] => 1 )