HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

preg_filter

Description

The preg_filter of PCRE for PHP perform a regular expression search and replace.

preg_filter() is identical to preg_replace() except it only returns the (possibly transformed) subjects where there was a match.

Syntax

 preg_filter(
    string|array $pattern,
    string|array $replacement,
    string|array $subject,
    int $limit = -1,
    int &$count = null
): string|array|null

Parameters

pattern

The pattern to search for. It can be either a string or an array with strings.

Several PCRE modifiers are also available.

replacement

The string or an array with strings to replace. If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. 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.

replacement may contain references of the form \n or $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. Note that backslashes in string literals may require to be escaped.

When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar \1 notation for your backreference. \11, for example, would confuse preg_replace() since it does not know whether you want the \1 backreference followed by a literal 1, or the \11 backreference followed by nothing. In this case the solution is to use ${1}1. This creates an isolated $1 backreference, leaving the 1 as a literal.

When using the deprecated e modifier, this function escapes some characters (namely ', ", \ and NULL) in the strings that replace the backreferences. This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. 'strlen('$1')+strlen("$2")'). Make sure you are aware of PHP's string syntax to know exactly how the interpreted string will look.

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

Returns an array if the subject parameter is an array, or a string otherwise.

If no matches are found or an error occurred, an empty array is returned when subject is an array or null otherwise.

Examples

1 · pattern replacement subject · string

<?

$pattern = '#dog#';
$replacement = 'alpaca';
$subject = 'The brown-and-white dog jumps over the other brown-and-white dog.';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
The brown-and-white alpaca jumps over the other brown-and-white alpaca.

2 · pattern replacement subject · array

<?

$pattern =
[
    '#dog#',
    '#brown#',
    '#white#'
];
$replacement =
[
    'alpaca',
    'black',
    'gray'
];
$subject =
[
    'The brown-and-white dog jumps over the other brown-and-white dog.',
    'The brown-and-white dog jumps over',
    'the other brown-and-white dog.'
];

$return = preg_filter($pattern, $replacement, $subject);

print_r($return);
Array
(
    [0] => The black-and-gray alpaca jumps over the other black-and-gray alpaca.
    [1] => The black-and-gray alpaca jumps over
    [2] => the other black-and-gray alpaca.
)

3 · limit

<?

$pattern =
[
    '#dog#',
    '#brown#',
    '#white#'
];
$replacement =
[
    'alpaca',
    'black',
    'gray'
];
$subject =
[
    'The brown-and-white dog jumps over the other brown-and-white dog.',
    'The brown-and-white dog jumps over',
    'the other brown-and-white dog.'
];
$limit = 1;

$return = preg_filter($pattern, $replacement, $subject, $limit);

print_r($return);
Array
(
    [0] => The black-and-gray alpaca jumps over the other brown-and-white dog.
    [1] => The black-and-gray alpaca jumps over
    [2] => the other black-and-gray alpaca.
)

4 · count

<?

$pattern =
[
    '#dog#',
    '#brown#',
    '#white#'
];
$replacement =
[
    'alpaca',
    'black',
    'gray'
];
$subject =
[
    'The brown-and-white dog jumps over the other brown-and-white dog.',
    'The brown-and-white dog jumps over',
    'the other brown-and-white dog.'
];
$limit = 1;

$return = preg_filter($pattern, $replacement, $subject, $limit, $count);

print_r($return);
echo $count;
ACDComment

5 · digit

<?

$pattern =
[
    '#\d#'
];
$replacement =
[
    '✓'
];
$subject =
[
    'abcdefghijklmnopqrstuvwxyz',
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    '1234567890',
    '!@#$%^&*()',
    '`-=[]\;,./',
    '~_+\{}|:"<>?',
    'match a digit 1776'
];

$return = preg_filter($pattern, $replacement, $subject);

print_r($return);
11: ACDComment
254: NewSubFile
255: SubFile
256: ImageWidth
257: ImageLength
258: BitsPerSample
259: Compression
262: PhotometricInterpretation
266: FillOrder
269: DocumentName
270: ImageDescription
271: Make
272: Model
273: StripOffsets
274: Orientation
277: SamplesPerPixel
278: RowsPerStrip
279: StripByteCounts
280: MinSampleValue
281: MaxSampleValue
282: XResolution
283: YResolution
284: PlanarConfiguration
285: PageName
286: XPosition
287: YPosition
288: FreeOffsets
289: FreeByteCounts
290: GrayResponseUnit
291: GrayResponseCurve
292: T4Options
293: T6Options
296: ResolutionUnit
297: PageNumber
301: TransferFunction
305: Software
306: DateTime
315: Artist
316: HostComputer
317: Predictor
318: WhitePoint
319: PrimaryChromaticities
320: ColorMap
321: HalfToneHints
322: TileWidth
323: TileLength
324: TileOffsets
325: TileByteCounts
330: SubIFD
332: InkSet
333: InkNames
334: NumberOfInks
336: DotRange
337: TargetPrinter
338: ExtraSample
339: SampleFormat
340: SMinSampleValue
341: SMaxSampleValue
342: TransferRange
343: ClipPath
344: XClipPathUnits
345: YClipPathUnits
346: Indexed
347: JPEGTables
351: OPIProxy
512: JPEGProc
513: JPEGInterchangeFormat
514: JPEGInterchangeFormatLength
515: JPEGRestartInterval
517: JPEGLosslessPredictors
518: JPEGPointTransforms
519: JPEGQTables
520: JPEGDCTables
521: JPEGACTables
529: YCbCrCoefficients
530: YCbCrSubSampling
531: YCbCrPositioning
532: ReferenceBlackWhite
700: ExtensibleMetadataPlatform
769: Gamma
770: ICCProfileDescriptor
771: SRGBRenderingIntent
800: ImageTitle

6 · non-digit

<?

$pattern =
[
    '#\D#',
];
$replacement =
[
    '✓',
];
$subject =
[
    'abcdefghijklmnopqrstuvwxyz',
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    '1234567890',
    '!@#$%^&*()',
    '`-=[]\;,./',
    '~_+\{}|:"<>?',
    'match a digit 1776'
];

$return = preg_filter($pattern, $replacement, $subject);

print_r($return);
Array
(
    [0] => ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓
    [1] => ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓
    [3] => ✓✓✓✓✓✓✓✓✓✓
    [4] => ✓✓✓✓✓✓✓✓✓✓
    [5] => ✓✓✓✓✓✓✓✓✓✓✓✓
    [6] => ✓✓✓✓✓✓✓✓✓✓✓✓✓✓1776
)

7 · word

<?

$pattern =
[
    '#\w#'
];
$replacement =
[
    '✓'
];
$subject =
[
    'abcdefghijklmnopqrstuvwxyz',
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    '1234567890',
    '!@#$%^&*()',
    '`-=[]\;,./',
    '~_+\{}|:"<>?',
    'match a word_'
];

$return = preg_filter($pattern, $replacement, $subject);

print_r($return);
Array
(
    [0] => ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓
    [1] => ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓
    [2] => ✓✓✓✓✓✓✓✓✓✓
    [5] => ~✓+\{}|:"<>?
    [6] => ✓✓✓✓✓ ✓ ✓✓✓✓✓
)

8 · non-word

<?

$pattern =
[
    '#\W#'
];
$replacement =
[
    '✓'
];
$subject =
[
    'abcdefghijklmnopqrstuvwxyz',
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    '1234567890',
    '!@#$%^&*()',
    '`-=[]\;,./',
    '~_+\{}|:"<>?',
    'match a word_'
];

$return = preg_filter($pattern, $replacement, $subject);

print_r($return);
Array
(
    [3] => ✓✓✓✓✓✓✓✓✓✓
    [4] => ✓✓✓✓✓✓✓✓✓✓
    [5] => ✓_✓✓✓✓✓✓✓✓✓✓
    [6] => match✓a✓word_
)

9 · ?

<?

$pattern = '#\w?#';
$replacement = '✓';
$subject = '0 or 1 time';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
✓✓ ✓✓✓ ✓✓ ✓✓✓✓✓

10 · *

<?

$pattern = '#\w*#';
$replacement = '✓';
$subject = '0 or more times';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
✓✓ ✓✓ ✓✓ ✓✓

11 · +

<?

$pattern = '#\w+#';
$replacement = '✓';
$subject = '1 or more times';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
✓ ✓ ✓ ✓

12 · whitespace

<?

$pattern = '#\s\s+#';
$replacement = ' ';
$subject = '    too  many   spaces';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
 too many spaces

13 · non-whitespace

<?

$pattern = '#\S\S+#';
$replacement = '✓';
$subject = '    too  many   spaces';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
    ✓  ✓   ✓

14 · group

<?

$pattern = '#(\w+) (\d+), (\d+)#i';
$replacement = '$1 1, $3';
$subject = 'January 31, 2001';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
January 1, 2001

15 · several

<?

$pattern =
[
    '#(19|20)(\d{2})-(\d{1,2})-(\d{1,2})#',
    '#^\s*{(\w+)}\s*=#'
];
$replacement =
[
    '\3/\4/\1\2',
    '$\1 ='
];
$subject = '{startDate} = 1999-9-29';

$return = preg_filter($pattern, $replacement, $subject);

echo $return;
$startDate = 9/29/1999

16 · compare preg_replace

<?

$pattern =
[
    '#\d#',
    '#[a-z]#',
    '#[1a]#'
];
$replacement =
[
    'A:$0',
    'B:$0',
    'C:$0'
];
$subject =
[
    '1',
    'a',
    '2',
    'b',
    '3',
    'A',
    'B',
    '4'
];

$return1 = preg_replace($pattern, $replacement, $subject);
$return2 = preg_filter($pattern, $replacement, $subject);

print_r($return1);
print_r($return2);
1776283233 = 2026-04-15 20:00:33