Table of Contents

Syntax

array preg_grep(string $pattern, array $source [, int $flags])

(PHP 4, PHP 5)

$pattern A POSIX regular expression string.
$source An array of values to test against the regular expression.
$flags The only option is PREG_GREP_INVERT which returns the opposite array (elements which DO NOT match the pattern).
RETURNS An array made up of elements from the $source array which match the pattern ($pattern).

What it Does

Given an input array ($source) and regular expression ($pattern), this function will build a new array from the elements in $source that match the supplied pattern.

Example

// create the input array (days of the week)
$wday = array('mon','tue','wed','thu','fri','sat','sun');
 
// get the weekend days from the array
$wendday = preg_grep('/^s.*/', $wday);