Función PHP preg_grep()
Ejemplo
Obtenga elementos de una matriz que comience con "p":
<?php
$input = [
"Red",
"Pink",
"Green",
"Blue",
"Purple"
];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
Definición y uso
La preg_grep()
función devuelve una matriz que contiene solo elementos de la entrada que coinciden con el patrón dado.
Sintaxis
preg_grep(pattern, input, flags)
Valores paramétricos
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. |
Detalles técnicos
Valor devuelto: | Devuelve una matriz que contiene cadenas que coincidieron con el patrón proporcionado |
---|---|
Versión PHP: | 4+ |
❮ Referencia PHP RegExp