Convert a String into an Array of Characters – multi-byte

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 = '先éé€𐍈💩👩‍ 👩‍❤️‍👩';

I expect:

array(16) {

  string(3) "先"
  string(2) "é"
  string(1) "e"
  string(2) "́"
  string(3) "€"
  string(4) "𐍈"
  string(4) "💩"
  string(4) "👩"
  string(3) "‍"
  string(1) " "
  string(4) "👩"
  string(3) "‍"
  string(3) "❤"
  string(3) "️"
  string(3) "‍"
  string(4) "👩"

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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)

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 => '漢',


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.

Dharman’s demo:

Solution 2

This works for me, it explodes a unicode string into an array of characters:

// split at all position not after the start: ^
// and not before the end: $, with unicode modifier
// u (PCRE_UTF8).
$arr = preg_split("/(?<!^)(?!$)/u", $text);

For example:

$text = "堆栈溢出";

$arr = preg_split("/(?<!^)(?!$)/u", $text);

echo '<html lang="fr">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />


echo '</body>

In a browser, it produces this:

Array ( [0] => 堆 [1] => 栈 [2] => 溢 [3] => 出 )

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply