array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'function.preg-grep.php', 1 => 'preg_grep', ), 'up' => array ( 0 => 'ref.pcre.php', 1 => 'PCRE Functions', ), 'prev' => array ( 0 => 'function.preg-filter.php', 1 => 'preg_filter', ), 'next' => array ( 0 => 'function.preg-last-error-msg.php', 1 => 'preg_last_error_msg', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/pcre/functions/preg-grep.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
preg_grep — Return array entries that match the pattern
Returns the array consisting of the elements of the
array
array that match the given
pattern
.
pattern
The pattern to search for, as a string.
array
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
.
Returns an array indexed using the keys from the
array
array, or false
on failure.
If the regex pattern passed does not compile to a valid regex, an E_WARNING
is emitted.
Example #1 preg_grep() example
<?php
// return all array elements
// containing floating point numbers
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
?>