Just pass an empty pattern with the PREG_SPLIT_NO_EMPTY
flag.
Otherwise, you can write a pattern with \X
(unicode dot) and \K
(restart fullstring match). I'll include a mb_split()
call and a preg_match_all()
call for completeness.
Code: (Demo)
$string='先秦兩漢';
var_export(preg_split('~~u', $string, 0, PREG_SPLIT_NO_EMPTY));
echo "\n---\n";
var_export(preg_split('~\X\K~u', $string, 0, PREG_SPLIT_NO_EMPTY));
echo "\n---\n";
var_export(preg_split('~\X\K(?!$)~u', $string));
echo "\n---\n";
var_export(mb_split('\X\K(?!$)', $string));
echo "\n---\n";
var_export(preg_match_all('~\X~u', $string, $out) ? $out[0] : []);
All produce::
array (
0 => '先',
1 => '秦',
2 => '兩',
3 => '漢',
)
From https://www.regular-expressions.info/unicode.html:
How to Match a Single Unicode Grapheme
Matching a single grapheme, whether it's encoded as a single code point, or as multiple code points using combining marks, is easy in Perl, PCRE, PHP, Boost, Ruby 2.0, Java 9, and the Just Great Software applications: simply use \X.
You can consider \X the Unicode version of the dot. There is one difference, though: \X always matches line break characters, whereas the dot does not match line break characters unless you enable the dot matches newline matching mode.
UPDATE, DHarman has brought to my attention that mb_str_split()
is now available from PHP7.4.
The default length parameter of the new function is 1, so the length parameter can be omitted for this case.
https://wiki.php.net/rfc/mb_str_split
Dharman's demo: https://3v4l.org/M85Fi/rfc#output
UPDATE (2024-04-10):
The RFC has unanimously passed for grapheme_str_split()
and is proposed for inclusion into PHP8.4. This provides a clean, native solution which will preserve bound multi-byte "clusters" (such emojis and variation selectors).
$string = '🙇♂️'
var_export(grapheme_str_split($string)); // ['🙇♂️']
Here is what the result would be if the cluster was not held together: (split on individual multibyte characters)
[
'🙇'
'', // U+200D Zero Width Joiner
'♂',
'', // U+FE0F Variation Selector
]
I'll add a 3v4l.org demo when possible.