PHP preg_grep()関数
例
「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歳以上 |
❮PHPRegExpリファレンス