PHP preg_grep() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Regex

Definition

In PHP, preg_grep() filters an array and returns only the elements whose string values match a regular expression.

It is like array_filter(), but the test is a PCRE pattern such as /\.txt$/. Keys are preserved. With PREG_GREP_INVERT you get the elements that did not match.

Syntax

Call preg_grep() with the values you need. Start with this line.

PHP
$hits = preg_grep('/\.txt$/', $files);
CallWhat it does
preg_grep('/\.txt$/', ['a.txt', 'b.jpg'])Returns an array containing only "a.txt".
preg_grep('/\d+/', ['x', 'y2', 'z'])Returns an array containing only "y2".
preg_grep('/\.txt$/', $files, PREG_GREP_INVERT)Returns the elements that do not end with .txt.

preg_grep() only filters; it does not change the text. Use preg_replace() or preg_filter() when you need to rewrite matching values.

Library declarations
PHP
preg_grep(string $pattern, array $array, int $flags = 0): array|false

Parameters

ParameterDescription
patternRequired. A PCRE pattern with delimiters, for example /\.jpg$/i.
arrayRequired. The input array. Each value is converted to a string for matching.
flagsOptional. Use PREG_GREP_INVERT to keep values that do not match. Default is 0.

Return Value

Return ValueDescription
array|falseAn array of matching elements (keys preserved), or false on failure such as a bad pattern.

PHP Examples

Example 1 — Keep File Names Ending in .txt

Filter a small list of file names with a pattern.

PHP
<?php
$files = ['a.txt', 'b.jpg', 'c.txt', 'd.png'];
$txt = preg_grep('/\.txt$/', $files);

print_r($txt);

How It Works

  1. The pattern /\. txt$/ means “ends with .txt”. The dots need escaping in regex.
  2. preg_grep() tests each array value against that pattern.
  3. a. txt and c.txt match, so they stay in the result.
  4. Original keys 0 and 2 are kept so you know which items matched.

Example 2 — Invert the Filter

Use PREG_GREP_INVERT to keep the items that did not match.

PHP
<?php
$files = ['a.txt', 'b.jpg', 'c.txt'];
$other = preg_grep('/\.txt$/', $files, PREG_GREP_INVERT);

print_r($other);

How It Works

  1. The same . txt pattern is used, but with PREG_GREP_INVERT.
  2. Matching names are dropped instead of kept.
  3. Only b. jpg remains.
  4. This is handy when you want “everything except …” without rewriting the pattern.

Example 3 — Find Lines That Contain Digits

Treat array values as short log lines and keep the ones with numbers.

PHP
<?php
$lines = ['ok', 'error 404', 'ready', 'code 7'];
$withDigits = preg_grep('/\d/', $lines);

print_r($withDigits);

How It Works

  1. The pattern /\d/ matches any digit character.
  2. ok and ready have no digits, so they are removed.
  3. error 404 and code 7 match and stay.
  4. Keys from the original array are preserved in the result.

Frequently Asked Questions

preg_grep() returns the elements of an array whose values match a regular expression. Non-matching elements are removed. Keys are preserved.
preg_grep() only filters; values stay unchanged. preg_filter() replaces matches and returns only the subjects that matched.
It reverses the test. You get the elements that did not match the pattern, instead of the ones that did.
No. It matches each top-level value as a string. Nested arrays are not walked. Flatten or map first if you need deeper checks.
preg_grep() returns false and PHP may emit a warning. Check preg_last_error() or preg_last_error_msg() after a failure.

Next: preg_last_error()

preg_last_error() returns an integer code for the last PCRE error from a preg_* function.

preg_last_error() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful