Wikis & learning

Choose between byte and character counts

A UTF-8 string stores some characters in several bytes. A storage limit, visible label limit and cursor position may each use a different unit.

A UTF-8 string stores some characters in several bytes. A storage limit, visible label limit and cursor position may each use a different unit.

Walkthrough

strlen reports the byte length. mb_strlen with UTF-8 counts code points in the example. Text containing combining marks or emoji sequences can need another layer of handling when a user-perceived character matters.

PHP example

<?php
$label = 'café';
echo strlen($label), ' bytes', PHP_EOL;
echo mb_strlen($label, 'UTF-8'), ' code points';

Expected output

5 bytes
4 code points

Change and check

Try a string containing a basic Latin letter followed by a Japanese character. Calculate both lengths before running the script. Enable mbstring for this lesson.