PHP preg_grep() Function
PHP Regular Expressions (PCRE)
The preg_grep function returns the array entries that match a pattern.
Syntax
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
Returns an array consisting of the elements of the given array input that match the pattern.
Parameter Description:
- $pattern: the pattern to search for, as a string.
- $input: the input array.
- $flags: if set to PREG_GREP_INVERT, this function returns an array of the elements in the input array that do not match the given pattern.
Example
Return the elements in the array that match the specified pattern:
The execution result is as follows:
Array
(
[2] => 3.4
[4] => 7.9
)
It can be seen that preg_grep only returned the floating-point numbers in the array.