从数组中获取以 "p" 开头的项目:
<?php
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
亲自试一试 »
这个preg_grep()
函数返回一个仅包含输入中与给定模式匹配的元素的数组。
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. |
返回值: | 返回一个包含与提供的模式匹配的字符串的数组 |
---|---|
PHP 版本: | 4+ |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!