Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

preg_grep(3) [php man page]

PREG_GREP(3)								 1							      PREG_GREP(3)

preg_grep - Return array entries that match the pattern

SYNOPSIS
array preg_grep (string $pattern, array $input, [int $flags]) DESCRIPTION
Returns the array consisting of the elements of the $input array that match the given $pattern. PARAMETERS
o $pattern - The pattern to search for, as a string. o $input - The input array. o $flags - If set to PREG_GREP_INVERT, this function returns the elements of the input array that do not match the given $pattern. RETURN VALUES
Returns an array indexed using the keys from the $input array. EXAMPLES
Example #1 preg_grep(3) example <?php // return all array elements // containing floating point numbers $fl_array = preg_grep("/^(d+)?.d+$/", $array); ?> SEE ALSO
PCRE Patterns, preg_quote(3), preg_match_all(3), preg_filter(3), preg_last_error(3). PHP Documentation Group PREG_GREP(3)

Check Out this Related Man Page

PREG_SPLIT(3)								 1							     PREG_SPLIT(3)

preg_split - Split string by a regular expression

SYNOPSIS
array preg_split (string $pattern, string $subject, [int $limit = -1], [int $flags]) DESCRIPTION
Split the given string by a regular expression. PARAMETERS
o $pattern - The pattern to search for, as a string. o $subject - The input string. o $limit - If specified, then only substrings up to $limit are returned with the rest of the string being placed in the last substring. A $limit of -1, 0 or NULL means "no limit" and, as is standard across PHP, you can use NULL to skip to the $flags parameter. o $flags -$flags can be any combination of the following flags (combined with the | bitwise operator): o PREG_SPLIT_NO_EMPTY - If this flag is set, only non-empty pieces will be returned by preg_split(3). o PREG_SPLIT_DELIM_CAPTURE - If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well. o PREG_SPLIT_OFFSET_CAPTURE - If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into $subject at offset 1. RETURN VALUES
Returns an array containing substrings of $subject split along boundaries matched by $pattern. EXAMPLES
Example #1 preg_split(3) example : Get the parts of a search string <?php // split the phrase by any number of commas or space characters, // which include " ", , , and f $keywords = preg_split("/[s,]+/", "hypertext language, programming"); print_r($keywords); ?> The above example will output: Array ( [0] => hypertext [1] => language [2] => programming ) Example #2 Splitting a string into component characters <?php $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars); ?> The above example will output: Array ( [0] => s [1] => t [2] => r [3] => i [4] => n [5] => g ) Example #3 Splitting a string into matches and their offsets <?php $str = 'hypertext language programming'; $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE); print_r($chars); ?> The above example will output: Array ( [0] => Array ( [0] => hypertext [1] => 0 ) [1] => Array ( [0] => language [1] => 10 ) [2] => Array ( [0] => programming [1] => 19 ) ) NOTES
Tip If you don't need the power of regular expressions, you can choose faster (albeit simpler) alternatives like explode(3) or str_split(3). Tip If matching fails, an array with a single element containing the input string will be returned. SEE ALSO
PCRE Patterns, preg_quote(3), implode(3), preg_match(3), preg_match_all(3), preg_replace(3), preg_last_error(3). PHP Documentation Group PREG_SPLIT(3)
Man Page