W3docs

preg_split

In PHP, regular expressions are an essential tool for manipulating and searching strings. The preg_split() function is one of the many functions that PHP

Introduction

preg_split() splits a string into an array of substrings, using a regular expression to describe the delimiter. It is the regex-powered cousin of explode(): where explode() can only split on one fixed string, preg_split() can split on a pattern — any run of whitespace, one of several punctuation characters, a digit boundary, and so on.

This page covers the function signature, every flag, the return value, and the practical patterns (and pitfalls) you'll hit when using it in real code.

Syntax

preg_split(
    string $pattern,
    string $subject,
    int $limit = -1,
    int $flags = 0
): array|false
ParameterDescription
$patternThe regular expression delimiter, including delimiters such as /.../.
$subjectThe input string to split.
$limitMaximum number of pieces. -1 (the default) or 0 means no limit. When set, the last piece holds the unsplit remainder.
$flagsBitmask of PREG_SPLIT_* constants (see below). Combine with `

The function returns an array of substrings on success, or false if the pattern is invalid. It never throws — check for false (and see preg_last_error()) if a split can fail.

A basic example

A classic use case is splitting a string on a delimiter that varies — here, any run of whitespace or commas:

php— editable, runs on the server

The pattern [\s,]+ matches one or more whitespace or comma characters, so both the spaces and the comma act as delimiters:

Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => test
    [4] => string
)

Because + is greedy, consecutive delimiters (a comma and a space) collapse into a single split point rather than producing empty pieces.

The flags

PREG_SPLIT_NO_EMPTY

Without this flag, a delimiter at the very start or end of the string — or two delimiters in a row that the pattern can't merge — produces empty strings in the result. PREG_SPLIT_NO_EMPTY drops them:

<?php

$string = ',apple,,banana,';

// Without the flag: empty pieces appear.
print_r(preg_split('/,/', $string));

// With the flag: only real values remain.
print_r(preg_split('/,/', $string, -1, PREG_SPLIT_NO_EMPTY));

The first call yields ['', 'apple', '', 'banana', '']; the second yields ['apple', 'banana'].

PREG_SPLIT_DELIM_CAPTURE

If the pattern contains capturing groups, this flag includes the captured text in the result — useful when you want to keep the delimiters, not just discard them:

<?php

$expression = '3+5*2-9';
$tokens = preg_split('/([+\-*\/])/', $expression, -1, PREG_SPLIT_DELIM_CAPTURE);

print_r($tokens);

The result keeps both the numbers and the operators: ['3', '+', '5', '*', '2', '-', '9'] — exactly what a tiny expression tokenizer needs.

PREG_SPLIT_OFFSET_CAPTURE

Each element becomes a [$substring, $offset] pair, where $offset is the byte position in the original string. Handy when you need to know where each piece came from.

Limiting the number of splits

$limit caps how many pieces you get back; the final element keeps the rest of the string unsplit. This is the idiomatic way to split "the first N fields, then everything else":

<?php

$logLine = 'ERROR 2024-01-01 Something broke: details here';

// Split into at most 3 parts on whitespace.
$parts = preg_split('/\s+/', $logLine, 3);

print_r($parts);

This produces ['ERROR', '2024-01-01', 'Something broke: details here'] — the third element retains its internal spaces because the limit was reached.

preg_split() vs explode()

Reach for explode() when the delimiter is a single fixed string — it is faster and clearer. Reach for preg_split() when the delimiter is a pattern: variable whitespace, a choice of characters, case-insensitive matching, or when you need to keep the delimiters via PREG_SPLIT_DELIM_CAPTURE. To rejoin an array back into a string, use implode().

Common pitfalls

  • Forgetting the pattern delimiters. The first argument is a full regex, so it needs delimiters: '/,/', not ','. Passing a bare string is the most common beginner mistake.
  • Unescaped special characters. Characters like ., +, |, and * are regex metacharacters. To split on a literal dot, escape it ('/\./') or use preg_quote() on dynamic input.
  • Stray empty strings. Leading/trailing delimiters create empty pieces — add PREG_SPLIT_NO_EMPTY if you don't want them.

Practice

Practice
What is the function of preg_split in PHP, and when should it be used?
What is the function of preg_split in PHP, and when should it be used?
Was this page helpful?