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

PHP preg_grep(): Detect Array Elements Matching A Given Pattern

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.

  1. 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);
    
  2. Applying regular expression filtering to arrays in PHP:

    • Use preg_grep() to filter array elements based on a regular expression.
    $pattern = "/^b/";
    $fruits = ["apple", "banana", "orange"];
    
    $filteredFruits = preg_grep($pattern, $fruits);
    
  3. Combining preg_grep() with other array functions in PHP:

    • Combine with other array functions for more complex operations.
    $pattern = "/^b/";
    $fruits = ["apple", "banana", "orange"];
    
    // Combine with array_map
    $uppercasedFilteredFruits = array_map('strtoupper', preg_grep($pattern, $fruits));
    
  4. Case-insensitive matching with preg_grep() in PHP:

    • Use the i flag for case-insensitive matching.
    $pattern = "/apple/i";
    $fruits = ["Apple", "banana", "orange"];
    
    $matchingFruits = preg_grep($pattern, $fruits);
    
  5. Handling multiple patterns and OR conditions with preg_grep() in PHP:

    • Provide an array of patterns for multiple conditions.
    $patterns = ["/apple/", "/banana/"];
    $fruits = ["apple", "banana", "orange"];
    
    $matchingFruits = preg_grep($patterns, $fruits);
    
  6. Filtering and extracting data from arrays with preg_grep() in PHP:

    • Use preg_grep() to filter and extract specific data from arrays.
    $pattern = "/^user_\d+/";
    $users = ["user_123", "admin", "user_456"];
    
    $filteredUsers = preg_grep($pattern, $users);
    
  7. Error handling and edge cases with preg_grep() in PHP:

    • Check for errors using 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();
    }