array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.preg-grep.php', 1 => 'preg_grep', 2 => 'Return array entries that match the pattern', ), 'up' => array ( 0 => 'ref.pcre.php', 1 => 'Функції PCRE', ), 'prev' => array ( 0 => 'function.preg-filter.php', 1 => 'preg_filter', ), 'next' => array ( 0 => 'function.preg-last-error.php', 1 => 'preg_last_error', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/pcre/functions/preg-grep.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($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.
patternThe pattern to search for, as a string.
arrayThe 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, або false в разі помилки.
Якщо задано хибний шаблон
регулярного виразу, виведеться повідомлення рівня
E_WARNING.
Приклад #1 preg_grep() example
<?php
$array = [ "4", M_PI, "2.74", 42 ];
// return all array elements containing floating point numbers
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
var_dump($fl_array);
?>