Oihana PHP

split.php

Table of Contents

Functions

split()  : array<string|int, mixed>
Splits a string into an array using a regular expression separator.

Functions

split()

Splits a string into an array using a regular expression separator.

split(string|null $source, string $separator[, int|null $limit = null ][, bool $ignoreCase = false ][, int|null $flags = null ][, bool $utf8 = false ]) : array<string|int, mixed>

This function is a wrapper around preg_split() with a simpler, more expressive API and optional UTF-8 support.

When $utf8 is enabled, the separator is evaluated using the /u modifier, allowing correct handling of multibyte characters.

By default, the separator is treated as a literal string. If you want to use a raw regular expression, pass it already delimited.

Examples

Basic usage:

split('foo,bar,baz', ',');      // ['foo', 'bar', 'baz']
split('a|b|c', '|');            // ['a', 'b', 'c']
split('one two  three', '\s+'); // ['one', 'two', 'three']

With limit:

split('a,b,c,d', ',', 2); // ['a', 'b,c,d']

Ignore case:

split('Foo|BAR|baz', 'bar', null, true); // ['Foo|', '|baz']

UTF-8:

split('éà üö', '\s+', null, false, null, true); // ['éà', 'üö']

Null or empty source:

split(null, ','); // []
split('', ',');   // []
Parameters
$source : string|null

The source string to split. If null, it is treated as an empty string.

$separator : string

The separator pattern or literal string.

$limit : int|null = null

If specified, limits the number of returned elements.

$ignoreCase : bool = false

Whether the separator matching should be case-insensitive.

$flags : int|null = null

Optional PREG_SPLIT_* flags.

$utf8 : bool = false

Whether to enable UTF-8 (/u) mode.

Tags
throws
InvalidArgumentException

If the separator is empty.

author

Marc Alcaraz (ekameleon)

since
1.0.8
Return values
array<string|int, mixed>

An array of split segments.


        
On this page

Search results