What is preg_filer() function?
If you want to search a string using a regular expression pattern and replace the match with your desire content, use preg_filter() function.
With this function, not only a string but, you can search an array of strings. Similarly, the searching regular expression pattern could be a string or an array. The function searches a string (the “subject” parameter) following a regular expression (the “pattern” parameter) and replace the match(s) with another content (the “replacement” parameter).
Syntax:
preg_filter(pattern, replacement, subject, limit, count)
Parameters:
pattern (Mandatory): It can either be a string (Check Example 1) or an array of strings (Check Example 2). This parameter is a regular expression that you’ll search to find matches.
replacement (Mandatory): It can either be a string or an array of string. This parameter is used to replace the matches that are found using the “pattern” parameter. Have a look at what happens if the “pattern” and “replacement” parameters are a string or an array.
- If this “replacement” parameter is a string and the “pattern” parameter is an array, all the matched strings found by the “pattern” parameter will be replaced by this parameter (Check Example 2).
- If both “Pattern” parameter and “replacement” parameter are arrays with equal elements, then each matched string found by each “pattern” parameter will be replaced by “replacement” parameter counterpart (Check Example 3).
- If both “pattern” parameter and “replacement” parameters are arrays but “replacement” array has fewer elements than “pattern” array, then the extra “pattern” will be replaced by an empty string (Check example 4).
subject (Mandatory): This can either be a string or an array. This is the main string where the function searches.
- If it is an array, the search and replace are performed on every element of the array and the return value is also an array (Check example 5).
- If it is an associative array, the keys are preserved in the returned value (Check example 6).
limit (Optional): It is a number that tell the maximum replacement number for each “pattern” in each “subject” string. Default number is -1 (which means no limit) (Check example 7).
count (Optional): if you specify this parameter as a variable, the variable stores number of replacement done by the function (Check example 8).
Return Values:
If no matches are found or an error occurred–
- It returns NULL (Check example 9), if the “subject” parameter is a string.
- It returns an empty array, if the “subject” parameter is an array (Check example 10).
If matches found-
- it returns a string (Check example 1), if the “subject” parameter is a string.
- It returns an array if the “subject” parameter is an array (Check example 5).
Examples:
Example 1:
<?php
$subject = 'Which function searches and replaces content';
$pattern = '/Which/';
$replacement = 'preg_filter';
$replace_str = preg_filter($pattern, $replacement, $subject);
echo $replace_str;
?>
Output:
preg_filter function searches and replaces content
Example 2:
<?php
$string = 'Both preg_filter and replace_callback are functions.';
$patterns = array('/function/');
$replacements = "built-in function";
echo preg_filter($patterns, $replacements, $string);
?>
Output:
Both preg_filter and replace_callback are built-in functions.
Example 3:
<?php
$string = 'preg_filter function this and that content';
$patterns = array('/this/', '/that/');
$replacements = array('searches', 'replaces');
echo preg_filter($patterns, $replacements, $string);
?>
Output:
preg_filter function searches and replaces content
Example 4:
<?php
$string = 'preg_filter function one, two, and three content';
$patterns = array('/one/', '/two/', '/three/');
$replacements = array('searches', 'replaces');
echo preg_filter($patterns, $replacements, $string);
?>
Output:
preg_filter function searches, replaces, and content
Example 5:
<?php
$string = array('preg_filter function this and replaces content', 'preg_match function this and matches content');
$patterns = array('/this/');
$replacements = array('searches');
$retnrn_str = preg_filter($patterns, $replacements, $string);
for($i=0; $i<count($retnrn_str); $i++){
echo $retnrn_str[$i]. "<br />";
}
?>
Output:
preg_filter function searches and replaces content
preg_match function searches and matches content
Example 6:
<?php
$string = array("One" => "preg_filter function this and replaces content", "Two" => "preg_match function this and matches content");
$patterns = array('/this/');
$replacements = array('searches');
$retnrn_str = preg_filter($patterns, $replacements, $string);
echo $retnrn_str["One"]. "<br />";
echo $retnrn_str["Two"]. "<br />";
?>
Output:
preg_filter function searches and replaces content
preg_match function searches and matches content
Example 7:
<?php
$subject = "There are three 'the' - the, the the";
$pattern = '/the/';
$replacement = 'one';
$replace_str = preg_filter($pattern, $replacement, $subject, 2);
echo $replace_str;
?>
Output:
There are three ‘one’ – one, the the
Example 8:
<?php
$subject = "There are three 'the' - the, the the";
$pattern = '/the/';
$replacement = 'one';
$replace_str = preg_filter($pattern, $replacement, $subject, -1, $count);
echo $count;
?>
Output:
4
Example 9:
<?php
$subject = 'Which function searches and replaces content';
$pattern = '/what/';
$replacement = 'preg_filter';
$replace_str = preg_filter($pattern, $replacement, $subject);
var_dump($replace_str);
?>
Output:
NULL
Example 10:
<?php
$string = array('preg_filter function this and replaces content', 'preg_match function this and matches content');
$patterns = array('/what/');
$replacements = array('searches');
$retnrn_str = preg_filter($patterns, $replacements, $string);
for($i=0; $i<count($retnrn_str); $i++){
var_dump($retnrn_str[$i]). "<br />";
}
?>
Output:
PHP Version Support:
PHP 5 >= 5.3.0, PHP 7, PHP 8
Summary: PHP preg_filter() Function
preg_filter() function searches a string using a regular expression pattern and replaces the match with your desire content. It is a built-in pcre function in PHP.