Signature
mb_split(string $pattern, string $string, int $limit = -1): array|falsePurpose
mb_split() cuts a string into pieces wherever a regular expression matches. It belongs to the mbstring regex family (mb_ereg() and friends), so it interprets the subject as characters in a multibyte encoding rather than as raw bytes. Use it when the separator is a pattern, such as "any run of whitespace" or "a comma or semicolon with optional spaces", and the text may contain non-ASCII characters.
Signature
Parameters and defaults
$pattern: the regular expression. It is written without delimiters, unlikepreg_split(). The text'\s+'is a complete pattern.$string: the text to split.$limit: the maximum number of elements in the result. The default-1means no limit. When the limit is reached, the last element holds the unsplit remainder.
The character encoding comes from mb_regex_encoding(). If your code depends on a specific encoding, set it explicitly at the start, for example with mb_regex_encoding('UTF-8').
Return value and failure
The function returns an array of strings on success and false on failure. Failure can happen, for example, when the pattern cannot be used. Check the result with === false before treating it as an array if the pattern comes from outside your code.
Example 1: a literal separator in UTF-8 text
mb_regex_encoding('UTF-8');
$parts = mb_split(',', 'α,β,γ');
echo json_encode($parts, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["α","β","γ"]
Each Greek letter is two bytes in UTF-8, but the split happens only at the comma, and each piece stays intact.
Example 2: limiting the number of pieces
mb_regex_encoding('UTF-8');
$parts = mb_split('\s+', "один два три четыре", 3);
echo json_encode($parts, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["один","два","три четыре"]
The pattern \s+ treats the double space as one separator. Because $limit is 3, the third element keeps the remaining words together as три четыре.
Example 3: a pattern with alternatives
mb_regex_encoding('UTF-8');
$parts = mb_split('\s*[;,]\s*', 'α ; β,γ ;δ');
echo json_encode($parts, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["α","β","γ","δ"]
The pattern consumes optional spaces around either a semicolon or a comma, so the resulting items need no trimming.
Edge cases
- Adjacent separators produce empty strings. Splitting
a,,bon a comma givesa, an empty string, andb. - A trailing separator produces a final empty string, as in
a,b,. - Patterns that match zero characters are risky as separators, and you should not rely on them to split text into individual characters. For that job, use
mb_str_split(). - There are no flags.
mb_split()has no equivalent ofPREG_SPLIT_NO_EMPTY. If empty items are unwanted, filter them afterwards, for example witharray_filter()and a strict length check. - A limit of -1 means unlimited. Treat other small values with care and test the behaviour you need before relying on it.
Common mistakes
- Adding delimiters. Writing
'/,/'makes the slashes part of the pattern, so it looks for the literal text/,/. Pass','instead. - Forgetting to escape metacharacters. The characters
.,|,+,*,?,(and[have special meaning. To split on a literal pipe, use'\|'. Use single-quoted PHP strings so the backslash reaches the regex engine unchanged. - Using it for plain separators. If the separator is a fixed string,
explode()is simpler and avoids regex interpretation. Remember thatexplode()works on bytes, which is safe for a fixed valid UTF-8 separator but gives no pattern matching. - Ignoring the encoding. If the regex encoding does not match the input, the results may be wrong or the call may fail. Keep input and
mb_regex_encoding()consistent. - Not checking for
false. Passing the result straight tocount()orforeachhides a failure. Test it first when the pattern is not a trusted constant. - Expecting
preg_split()features. Capturing delimiters, offsets and flags belong topreg_split(), not tomb_split().
Related functions
mb_regex_encoding()gets or sets the encoding used by the mbstring regex functions.mb_ereg()tests a multibyte string against a regex pattern.mb_str_split()splits a string into characters or fixed-size character chunks.explode()splits by a literal string and works on bytes.preg_split()offers PCRE patterns, delimiters, and flags such as dropping empty pieces.
Recap
mb_split() splits text on a delimiter-free regular expression and returns an array or false. Set the regex encoding explicitly, escape special characters, use $limit when the tail should stay in one piece, and remember that empty elements appear around adjacent or trailing separators.
Tested examples
mb_regex_encoding('UTF-8');
$parts = mb_split(',', 'α,β,γ');
echo json_encode($parts, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["α","β","γ"]
mb_regex_encoding('UTF-8');
$parts = mb_split('\s+', "один два три четыре", 3);
echo json_encode($parts, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["один","два","три четыре"]
mb_regex_encoding('UTF-8');
$parts = mb_split('\s*[;,]\s*', 'α ; β,γ ;δ');
echo json_encode($parts, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["α","β","γ","δ"]