Assuming that in 2019 every solution which is not UNICODE-safe is wrong. What is the best way to convert a string to array of UNICODE characters in PHP?
Obviously this means that accessing the bytes with the brace syntax is wrong, as well as using str_split
:
$arr = str_split($text);
From sample input like:
$string = '先éé€
This works for me, it explodes a unicode string into an array of characters:
For example:
In a browser, it produces this:
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 amb_split()
call and apreg_match_all()
call for completeness.Code: (Demo)
All produce::
From https://www.regular-expressions.info/unicode.html:
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