Easy Tutorial
❮ Func Cal Jddayofweek Func Libxml Get Errors ❯

PHP preg_grep() Function

PHP Regular Expressions (PCRE)

The preg_grep function is used to return array entries that match the pattern.

Syntax

array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

Returns an array of elements from the given array input that match the pattern.

Parameter Descriptions:

Example

Return Elements of an Array Matching a Specific Pattern:

<?php
$array = array(1, 2, 3.4, 53, 7.9);
// Return all elements that are floating point numbers
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
print_r($fl_array);
?>

The execution result is as follows:

Array
(
    [2] => 3.4
    [4] => 7.9
)

It can be seen that preg_grep only returns the floating point numbers from the array.

PHP Regular Expressions (PCRE)

❮ Func Cal Jddayofweek Func Libxml Get Errors ❯