Current Location: Home> Function Categories> preg_grep

preg_grep

Returns array entries that match pattern
Name:preg_grep
Category:Regular processing PCRE
Programming Language:php
One-line Description:Returns an array that contains only elements of the input array that match the pattern.

Definition and usage

preg_grep() function returns an array containing only elements in the input that match the given pattern.

Example

Get items starting with "p" from the array:

 <?php
$input = [
  "Red" ,
  "Pink" ,
  "Green" ,
  "Blue" ,
  "Purple"
] ;

$result = preg_grep ( "/^p/i" , $input ) ;
print_r ( $result ) ;
?>

Try it yourself

grammar

 preg_grep ( pattern , input , flags )
parameter describe
pattern Required. Contains regular expressions indicating what to search for.
input Required. String array.
flags Elective. This function has only one flag. Passing the constant PREG_GREP_INVERT will cause the function to return only items that do not match the pattern.
Similar Functions
Popular Articles