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_filter(): Perform A Regular Expression Search And Replace

The preg_filter() function in PHP is used to perform a regular expression search and replace. Unlike preg_replace(), preg_filter() returns null if no match is found or nothing was replaced.

Here is a basic tutorial on how to use the preg_filter() function in PHP:

Syntax:

The syntax of preg_filter() is:

preg_filter(mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) : array|string|null
  • $pattern: The pattern to search for. It can be a string or an array with strings.
  • $replacement: The string or an array with strings to replace. If this parameter is a string and the $pattern parameter is an array, then this string is used as a replacement for every pattern. If both $pattern and $replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the $replacement array than in the $pattern array, any extra $patterns will be replaced by an empty string.
  • $subject: The string or an array with strings to search and replace. If $subject is an array, then the search and replace is performed on every entry of $subject, and the return value is an array as well.
  • $limit: The maximum possible replacements for each pattern in each $subject string. Defaults to -1 (no limit).
  • &$count: If specified, this variable will be filled with the number of replacements done.

Return Value:

Returns an array if the $subject parameter is an array, otherwise it returns a string.

If no matches are found or if replacements were not performed, then it returns null.

Example:

Here is an example demonstrating how to use preg_filter():

<?php
    $pattern = '/(\b[a-z])/i';
    $replacement = '[$1]';
    $subject = "hello world";

    // using preg_filter()
    $result = preg_filter($pattern, $replacement, $subject);

    // Output: [h]ello [w]orld
    echo $result;
?>

In this example, the $pattern is a regular expression that matches the first letter of each word in the $subject string. The preg_filter() function then replaces each match with the corresponding $replacement, resulting in each first letter of each word being enclosed in square brackets.

Note: The $1 in the replacement string is a backreference to the first (and only) capture group in the pattern, which is the first letter of each word.

  1. Using preg_filter() for regular expression search and replace in PHP:

    • preg_filter() performs a regular expression search and replace on an array.
    $pattern = "/apple/";
    $replacement = "orange";
    $fruits = ["apple", "banana", "apple"];
    
    $newFruits = preg_filter($pattern, $replacement, $fruits);
    
  2. Multi-dimensional arrays and preg_filter() in PHP:

    • preg_filter() can handle multi-dimensional arrays.
    $pattern = "/apple/";
    $replacement = "orange";
    $fruits = [
        ["apple", "banana", "apple"],
        ["orange", "grape", "apple"]
    ];
    
    $newFruits = preg_filter($pattern, $replacement, $fruits);
    
  3. Applying multiple search and replace patterns with preg_filter():

    • Use arrays for multiple patterns and replacements.
    $patterns = ["/apple/", "/banana/"];
    $replacements = ["orange", "grape"];
    $fruits = ["apple", "banana", "apple"];
    
    $newFruits = preg_filter($patterns, $replacements, $fruits);
    
  4. Filtering and transforming data with regular expressions in PHP:

    • Use regular expressions to filter and transform data.
    $pattern = "/[0-9]+/";
    $replacement = "X";
    $text = "There are 123 apples and 456 bananas.";
    
    $transformedText = preg_replace($pattern, $replacement, $text);
    
  5. Combining preg_filter() with preg_match() and preg_replace() in PHP:

    • Use preg_match() to filter based on a pattern and then apply replacements with preg_replace().
    $pattern = "/apple/";
    $replacement = "orange";
    $fruits = ["apple", "banana", "apple"];
    
    foreach ($fruits as $fruit) {
        if (preg_match($pattern, $fruit)) {
            $newFruit = preg_replace($pattern, $replacement, $fruit);
            echo $newFruit . "<br>";
        }
    }
    
  6. Handling flags and options with preg_filter() in PHP:

    • Specify flags and options as the last parameter.
    $pattern = "/apple/i"; // Case-insensitive
    $replacement = "orange";
    $fruits = ["apple", "banana", "APPLE"];
    
    $newFruits = preg_filter($pattern, $replacement, $fruits, PREG_IGNORE_CASE);
    
  7. Error handling and debugging with preg_filter() in PHP:

    • Check for errors using preg_last_error().
    $pattern = "/apple/";
    $replacement = "orange";
    $fruits = ["apple", "banana", "apple"];
    
    $newFruits = preg_filter($pattern, $replacement, $fruits);
    
    if (preg_last_error() !== PREG_NO_ERROR) {
        echo "Error: " . preg_last_error();
    }