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);
 
preg_grep.txt · Last modified: 2008/04/18 14:01 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki