Função PHP preg_grep()
Exemplo
Obter itens de uma matriz que começam com "p":
<?php
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
Definição e uso
A preg_grep()
função retorna uma matriz contendo apenas elementos da entrada que correspondem ao padrão fornecido.
Sintaxe
preg_grep(pattern, input, flags)
Valores de parâmetro
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. |
Detalhes técnicos
Valor de retorno: | Retorna um array contendo strings que correspondem ao padrão fornecido |
---|---|
Versão do PHP: | 4+ |
❮ Referência PHP RegExp