Finalist Blog: Use case for multibyte safe programming

Drupal CMS

In PHP, a technique to get a single character from a string is to treat the string as an array, or to use a function like sub⁠­str().

$string = ‘This is a string’;
$string[0]; // first character: T
$string[1]; // second: h
$string[-1]; // last: g

This also works in Javascript. A big difference with Javascript is that PHP is not doing this in a ‘multibyte safe’ way. More on this later on. See:

$string1 = ‘Café’;
$string2 = ‘Österreich’;
$string1[0]; // C
$string1[-1]; // �
$string2[0]; // �
$string2[-1]; // h

If these characters are part of a JSON, they could end up breaking…