PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
The preg_grep()
function in PHP is used to search an array for items that match a regular expression pattern.
Here is a basic tutorial on how to use the preg_grep()
function in PHP:
Syntax:
The syntax of preg_grep()
is:
preg_grep(string $pattern , array $input [, int $flags = 0 ]) : array
$pattern
: The pattern to search for. It should be a string.$input
: The input array.$flags
: It's optional. If set to PREG_GREP_INVERT
, this function returns the elements of the input array that do not match the given pattern.Return Value:
Returns an array indexed using the keys from the input array.
Example:
Here is an example demonstrating how to use preg_grep()
:
<?php $pattern = '/^a/i'; $input = array("Apple", "Banana", "Avocado", "Cherry", "Apricot", "Mango"); // using preg_grep() $result = preg_grep($pattern, $input); // Output: Array ( [0] => Apple [2] => Avocado [4] => Apricot ) print_r($result); ?>
In this example, the $pattern
is a regular expression that matches strings beginning with the letter 'a'. The preg_grep()
function then searches for the pattern in the $input
array and returns an array containing the items that match the pattern.
Inverted Search:
We can also use the PREG_GREP_INVERT
flag to return items that do not match the pattern.
<?php $pattern = '/^a/i'; $input = array("Apple", "Banana", "Avocado", "Cherry", "Apricot", "Mango"); // using preg_grep() with PREG_GREP_INVERT flag $result = preg_grep($pattern, $input, PREG_GREP_INVERT); // Output: Array ( [1] => Banana [3] => Cherry [5] => Mango ) print_r($result); ?>
In this example, the $pattern
is a regular expression that matches strings beginning with the letter 'a'. The preg_grep()
function then searches for the pattern in the $input
array and returns an array containing the items that do not match the pattern, since the PREG_GREP_INVERT
flag was set.
Using preg_grep()
to detect array elements matching a pattern in PHP:
preg_grep()
filters array elements based on a regular expression pattern.$pattern = "/apple/"; $fruits = ["apple", "banana", "orange"]; $matchingFruits = preg_grep($pattern, $fruits);
Applying regular expression filtering to arrays in PHP:
preg_grep()
to filter array elements based on a regular expression.$pattern = "/^b/"; $fruits = ["apple", "banana", "orange"]; $filteredFruits = preg_grep($pattern, $fruits);
Combining preg_grep()
with other array functions in PHP:
$pattern = "/^b/"; $fruits = ["apple", "banana", "orange"]; // Combine with array_map $uppercasedFilteredFruits = array_map('strtoupper', preg_grep($pattern, $fruits));
Case-insensitive matching with preg_grep()
in PHP:
i
flag for case-insensitive matching.$pattern = "/apple/i"; $fruits = ["Apple", "banana", "orange"]; $matchingFruits = preg_grep($pattern, $fruits);
Handling multiple patterns and OR conditions with preg_grep()
in PHP:
$patterns = ["/apple/", "/banana/"]; $fruits = ["apple", "banana", "orange"]; $matchingFruits = preg_grep($patterns, $fruits);
Filtering and extracting data from arrays with preg_grep()
in PHP:
preg_grep()
to filter and extract specific data from arrays.$pattern = "/^user_\d+/"; $users = ["user_123", "admin", "user_456"]; $filteredUsers = preg_grep($pattern, $users);
Error handling and edge cases with preg_grep()
in PHP:
preg_last_error()
.$pattern = "/[a-z/"; $fruits = ["apple", "banana", "orange"]; $matchingFruits = preg_grep($pattern, $fruits); if (preg_last_error() !== PREG_NO_ERROR) { echo "Error: " . preg_last_error(); }