Home > Article > Backend Development > How to Iterate through UTF-8 Strings Character by Character in PHP
When working with UTF-8 strings in PHP, accessing characters through indexing can yield unexpected results due to multibyte encoding. The question focuses on the challenge of iterating through a UTF-8 string character by character while maintaining character integrity.
To overcome the limitations of using the bracket operator, the solution lies in utilizing preg_split. By employing the "u" modifier, preg_split supports UTF-8 unicode and enables accurate splitting of characters:
<code class="php">$str = "Kąt"; $chrArray = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);</code>
This results in the following character array:
["K", "ą", "t"]
By leveraging preg_split, you can effectively iterate through a UTF-8 string character by character, retaining the integrity of multibyte characters and avoiding the performance penalty associated with mb_substr.
The above is the detailed content of How to Iterate through UTF-8 Strings Character by Character in PHP. For more information, please follow other related articles on the PHP Chinese website!