PHP preg_split() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Regex

Definition

In PHP, preg_split() splits a string into an array using a regular expression as the delimiter.

It is like explode(), but the separator is a pattern such as /\s+/. Flags like PREG_SPLIT_NO_EMPTY drop empty pieces, and PREG_SPLIT_DELIM_CAPTURE keeps capturing groups from the delimiter.

Syntax

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

PHP
$parts = preg_split('/\s+/', $line);
CallWhat it does
preg_split('/\s+/', 'one two')Returns ["one", "two"].
preg_split('/,/', 'a,,b', -1, PREG_SPLIT_NO_EMPTY)Returns ["a", "b"] without empty pieces.
preg_split('/\d+/', 'a1b2c')Returns ["a", "b", "c"].

Use a limit to stop after N pieces (the last element holds the remainder). PREG_SPLIT_OFFSET_CAPTURE also returns byte offsets for each piece.

Library declarations
PHP
preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|false

Parameters

ParameterDescription
patternRequired. PCRE pattern that matches the delimiter.
subjectRequired. The string to split.
limitOptional. Maximum number of pieces. -1 means no limit. 1 returns the whole string as a one-element array.
flagsOptional. PREG_SPLIT_NO_EMPTY, PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE.

Return Value

Return ValueDescription
array|falseAn array of substrings, or false on failure.

PHP Examples

Example 1 — Split on Whitespace

Cut a sentence into words using a whitespace pattern.

PHP
<?php
$parts = preg_split('/\s+/', 'one  two   three');
print_r($parts);

How It Works

  1. The pattern /\s+/ matches one or more spaces.
  2. Multiple spaces count as a single delimiter.
  3. The string becomes three words.
  4. print_r() shows the resulting array.

Example 2 — Drop Empty Pieces

Compare a split that keeps empty strings with PREG_SPLIT_NO_EMPTY.

PHP
<?php
print_r(preg_split('/,/', 'a,,b'));
print_r(preg_split('/,/', 'a,,b', -1, PREG_SPLIT_NO_EMPTY));

How It Works

  1. Two commas in a row create an empty piece between them.
  2. The first print_r shows a, an empty string, and b.
  3. PREG_SPLIT_NO_EMPTY removes that empty piece.
  4. The second result is only a and b.

Example 3 — Keep the Delimiter with DELIM_CAPTURE

Capture digits in the pattern so the split keeps them in the result.

PHP
<?php
$parts = preg_split('/(\\d+)/', 'a1b2c', -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($parts);

How It Works

  1. Parentheses around \d+ make the digits a capturing group.
  2. PREG_SPLIT_DELIM_CAPTURE inserts those captured delimiters into the array.
  3. Letters and digits alternate in the result.
  4. Without that flag, only a, b and c would remain.

Frequently Asked Questions

It splits a string into an array using a regular expression as the delimiter, instead of a fixed string like explode().
It removes empty strings from the result, which often appear when delimiters are next to each other.
If the pattern has capturing parentheses, the captured delimiter text is included in the returned array.
When the separator is a fixed string with no pattern needed. Use preg_split() when the delimiter is flexible, such as any whitespace.
preg_split() returns false. Check preg_last_error() or preg_last_error_msg() for details.

Back to Regex Functions

Browse all PHP regex function tutorials.

Regex functions →

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