How to Use preg_split in PHP
This article explains the purpose and usage of the
preg_split() function in PHP. You will learn how this
built-in function splits strings into arrays using regular expressions,
explore its syntax and key parameters, and see practical examples of how
to implement it in your PHP applications.
What is preg_split()?
In PHP, the preg_split() function is used to split a
given string into an array of substrings using a regular expression as
the delimiter.
Unlike the simpler explode() function, which only splits
strings using a static, literal string delimiter (such as a single comma
or a space), preg_split() allows you to define complex,
dynamic search patterns. This makes it highly effective for parsing data
with inconsistent formatting, such as text separated by varying amounts
of whitespace or multiple different punctuation marks.
Syntax of preg_split()
The basic syntax of the function is as follows:
preg_split(
string $pattern,
string $subject,
int $limit = -1,
int $flags = 0
): array|falseParameters:
$pattern: The regular expression pattern to search for, represented as a string.$subject: The input string that you want to split.$limit: (Optional) The maximum number of substrings to return. The default is-1(no limit).$flags: (Optional) Special flags to alter the behavior of the split.
Common Flags:
PREG_SPLIT_NO_EMPTY: If this flag is set, only non-empty pieces will be returned bypreg_split().PREG_SPLIT_DELIM_CAPTURE: If set, the matched pattern inside parentheses in the delimiter expression will be captured and returned as part of the array.PREG_SPLIT_OFFSET_CAPTURE: If set, for every occurring match, the appendant string offset will also be returned in the array.
Practical Examples
1. Splitting a String by Any Amount of Whitespace
If you have a string where words are separated by an inconsistent
number of spaces, tabs, or newlines, preg_split() can clean
it up instantly using the \s+ pattern.
$string = "PHP is a popular scripting language.";
$words = preg_split("/\s+/", $string);
print_r($words);Output:
Array
(
[0] => PHP
[1] => is
[2] => a
[3] => popular
[4] => scripting
[5] => language.
)
2. Splitting by Multiple Delimiters
You can use the pipe character (|) in your regular
expression to split a string by multiple different characters, such as
commas, semicolons, and vertical bars.
$string = "apple,orange;banana|grape";
$fruits = preg_split("/[,;|]/", $string);
print_r($fruits);Output:
Array
(
[0] => apple
[1] => orange
[2] => banana
[3] => grape
)
3. Using the PREG_SPLIT_NO_EMPTY Flag
When splitting strings, you may end up with empty array elements if
delimiters are next to each other. The PREG_SPLIT_NO_EMPTY
flag automatically filters these out.
$string = "one,,two,,,three";
// Without the flag, empty strings would be in the array
$result = preg_split("/,/", $string, -1, PREG_SPLIT_NO_EMPTY);
print_r($result);Output:
Array
(
[0] => one
[1] => two
[2] => three
)
Summary
The preg_split() function is a powerful tool in PHP for
string manipulation. While explode() is faster and
preferred for simple, static delimiters, preg_split() is
the industry standard when you need to parse strings using complex
conditions, multiple delimiters, or variable spacing.