Get items from an array which begin with "p":
<?php
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
Try it Yourself »
The preg_grep()
function returns an array containing only elements from the input that match the given pattern.
preg_grep(
pattern, input, flags)
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. An array of strings |
flags | Optional. There is only one flag for this function. Passing the constant PREG_GREP_INVERT will make the function return only items that do not match the pattern. |
Return Value: | Returns an array containing strings that matched the provided pattern |
---|---|
PHP Version: | 4+ |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!