Using str_split on a UTF-8 encoded string

Using str_split on a UTF-8 encoded string

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Using str_split on a UTF-8 encoded string

When working with multilingual data in PHP, the standard str_split() function often feels like a blunt instrument. If you have successfully fetched a string containing characters like åäö from a MySQL database and verified it displays correctly in your browser or via var_dump, you might assume the data is safe to process. However, splitting that same string into an array can suddenly result in garbled question marks (�) and duplicated entries. This discrepancy isn't a bug in PHP's string handling; it is a mismatch between how characters are stored internally versus how they are interpreted by the splitting function.

The root cause lies in the distinction between bytes and code points. In UTF-8, single-byte characters like 'T' or 'e' occupy one byte, while multi-byte characters like 'å' can occupy two or three. The str_split() function operates on bytes, not Unicode code points. When you pass a string containing multi-byte characters to str_split(), the function splits at every byte boundary. If the split occurs in the middle of a multi-byte character sequence, PHP cannot interpret that fragment as a valid character. It defaults to displaying a replacement character (�) and effectively truncates the logical character, often leading to the duplication effect seen when the next iteration of the loop or split attempts to reconstruct the remaining bytes.

To resolve this, you must ensure the string is converted into a format where characters are atomic units before splitting. The most robust solution in modern PHP (7.0+) is to use mb_str_split(). This function respects the current internal encoding and splits strictly at code point boundaries, ensuring that a character like 'Ã¥' is treated as a single unit regardless of its byte length.

Here is how you can adjust your code to handle the splitting correctly:

// Assuming $artist->name holds the string fetched from the database
$rawName = $artist->name;

// Use mb_str_split instead of str_split for UTF-8 safety
$chars = mb_str_split($rawName);

var_dump($chars); 
// Output will now correctly show: ['T', 'e', 's', 't', ' ', '', 'Ä', 'Ö', ' ', 'T', 'å', 'ä', 'n']

If you are constrained to older PHP versions or cannot use mb_str_split(), an alternative approach involves converting the string to UTF-8 code points first. You can achieve this by iterating over the string using mb_strlen() with the UTF-8 flag and extracting substrings of length 1 based on the character count rather than byte count. However, mb_str_split() is generally preferred for its readability and performance optimizations built into the PHP engine.

It is also worth noting that your database connection settings are correctly configured. Using charset=utf-8 in the DSN and setting the client character set to utf8 ensures that PDO retrieves the data with the correct byte sequences intact. The issue arises strictly during the string manipulation phase, not the retrieval phase. As long as the source file is UTF-8 without BOM (which you have confirmed), the PHP interpreter will handle the input correctly up until the point str_split() intervenes.

For developers using the Laravel framework, this specific issue rarely occurs because Laravel's underlying helpers often leverage multibyte functions by default or use stringable traits that handle encoding explicitly. If you are building a solution that needs to be framework-agnostic, sticking to mb_str_split() is the industry-standard practice for dealing with internationalized user input.

Before shuffling the array, always ensure the resulting array contains only valid characters. If your application logic depends on these characters being shuffled randomly (e.g., creating a random name puzzle), verify that the shuffle operation does not inadvertently reorder bytes across multi-byte boundaries if you were to process the string again after splitting. The safest workflow is: fetch -> split with mb_str_split -> shuffle array -> join back to string only when necessary for display or storage.

Our network