Understanding PHP preg_filter: Syntax, Parameters, and Examples

The article explains PHP's preg_filter function, detailing its signature, parameters, return values, and differences from preg_replace, and provides clear code examples that demonstrate how it returns only matched results while omitting non‑matching elements.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Understanding PHP preg_filter: Syntax, Parameters, and Examples

preg_filter() is a PHP function that performs a regular‑expression search and replace, returning only the parts of the subject that match the pattern.

It is equivalent to preg_replace() except that it returns only the transformed matches; non‑matching elements are omitted. The function signature is

preg_filter(mixed $pattern, mixed $replacement, mixed $subject, int $limit = -1, int &$count = null)

.

Parameters

pattern : the regex pattern(s) to search for (string or array).

replacement : the replacement string(s) (string or array).

subject : the input string or array to be processed.

limit : maximum number of replacements per pattern; default -1 (no limit).

count : optional variable that receives the total number of replacements performed.

Return value

If $subject is an array, an array of matched results is returned; otherwise a string is returned. If no matches are found or an error occurs, NULL (or an empty array for array subjects) is returned.

Example 1

<?php
$subject = array('1','a','2','b','3','A','4');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0','B:$0','C:$0');

echo "preg_filter 返回值:
";
print_r(preg_filter($pattern, $replace, $subject));

echo "preg_replace 返回值:
";
print_r(preg_replace($pattern, $replace, $subject));
?>

The output shows that preg_filter returns only the elements that matched the patterns, while preg_replace returns the full array with non‑matching elements unchanged.

Example 2

<?php
$subject = array('1','a','2','b','3','A','4');
$result = preg_filter('/\d/', '$0', $subject);
print_r($result);
?>

This example demonstrates a simple use of preg_filter to extract numeric characters from an array.

In summary, preg_filter is useful when you need only the matched portions of a string or array after applying regular‑expression replacements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendregexstring-replacementphp-functionspreg_filter
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.