Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Regular Expressions

PHP preg_split() function

Reference for PHP preg_split(): split a string with a regular expression, use the limit and the NO_EMPTY, DELIM_CAPTURE and OFFSET_CAPTURE flags, and avoid common traps.

The preg_split() function splits a string into an array of pieces wherever a regular expression matches. It is explode() with a pattern as the separator, so it can split on "a comma with optional spaces around it", on any run of whitespace, or on several different separators at once.

With flags you can drop empty pieces, keep the separators that were captured, or get the position of every piece.

PHP
<?php
print_r(preg_split('/\s*,\s*/', 'a , b,c'));

Try it

PHP Splitting on patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Keeping delimiters and offsets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
preg_split(
    string $pattern,
    string $subject,
    int $limit = -1,
    int $flags = 0
): array|false
Name Description
$pattern The regular expression, with delimiters and modifiers.
$subject The string to split.
$limit Maximum number of pieces. -1 or 0 means no limit. With a limit, the last piece holds the unsplit rest.
$flags Any combination (with |) of the flags below.
Flag Effect
PREG_SPLIT_NO_EMPTY Leaves out empty pieces. The limit then counts only non-empty pieces.
PREG_SPLIT_DELIM_CAPTURE Adds the text of captured groups in the pattern between the pieces.
PREG_SPLIT_OFFSET_CAPTURE Returns each piece as [text, byte offset].

Return value: an array of strings (or [text, offset] pairs), or false on failure.

Errors / Exceptions: an invalid pattern emits a warning and returns false. Invalid UTF-8 with /u or an exceeded backtrack limit also returns false; see preg_last_error().

Usage notes

  • Empty pieces. A separator at the start or end, or two separators in a row, creates empty strings. Add PREG_SPLIT_NO_EMPTY when you only want the content.
  • No match returns an array with one element, the whole subject.
  • An empty pattern (//) splits between characters, with empty pieces at both ends. Use //u with NO_EMPTY for UTF-8 text, or mb_str_split().
  • Zero-width matches such as lookaheads split without consuming any text, which is how the camelCase example works.
  • Plain separators are faster and simpler with explode(). Use preg_split() only when the separator has variable shape.
  • Offsets are in bytes, even with /u.
  • Compare with false, not truthiness: an empty array cannot happen (there is always at least one piece), but a failed split is false.
  • Fixed-width chunks do not need a regex: use str_split().