preg_grep
Returns array entries that match pattern
preg_grep()
function returns an array containing only elements in the input that match the given pattern.
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
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. |