...

/

Accessing Individual Characters in a UTF-8 String

Accessing Individual Characters in a UTF-8 String

Understanding the details of accessing individual characters in a UTF-8 string.

We'll cover the following...

A fully functional class can iterate UTF-8 strings and return the correct number of characters when using PHP’s count function. Currently, if we attempt to do the following, we will receive errors:

Press + to interact
<?php
$iterator = new Utf8StringIterator('這可以-this is fine!');
$iterator[0];
$iterator[1];
$iterator[2] = 'A';

It would be nice to be able to access, set, or remove characters at arbitrary positions using PHP’s array access syntax. We can accomplish this by implementing another PHP interface: the ArrayAccess interface.

The ArrayAccess interface

To implement this interface, we can make the following additions. A lot is going on with these additions, but we will cover all of the crucial parts ...