Multibyte strings

A UTF-8 character may take several bytes, and the byte functions of PHP count the bytes. The mbstring extension counts characters instead, which is what a reader of the text counts.

mb_strlen

Function strlen counts bytes while mb_strlen counts characters. For plain English both agree, but for accented letters and emoji only mb_strlen is correct.

Always pass the encoding explicitly in lessons and tools so the result does not depend on server defaults.

<?php $text = "caf\u{E9}"; echo strlen($text), PHP_EOL; echo mb_strlen($text, "UTF-8"), PHP_EOL; echo bin2hex($text), PHP_EOL;

php length.php

5
4
636166c3a9

The word has four characters, but the é is written as two bytes, so the byte count is five; the last line shows those bytes in hexadecimal.

mb_substr

Function substr can split a multibyte character in half and produce broken output. Function mb_substr cuts by characters and keeps the text intact.

Offsets start at zero, so position one means the second character. Length tells how many characters to keep.

<?php $text = "h\u{E9}llo"; echo mb_substr($text, 1, 3, "UTF-8"), PHP_EOL; echo mb_substr($text, 0, 2, "UTF-8"), PHP_EOL;

php substr.php

éll
hé

The first cut starts at the second character and keeps three, the second keeps the first two, and the accented letter comes out whole in both.

Detect encoding

Text from files and forms may arrive in an unknown encoding. Function mb_detect_encoding guesses the encoding from a short list that you provide.

Treat the guess as a hint and validate before storage. Strict mode returns false when the bytes do not fit any listed encoding.

<?php $text = "hello"; var_dump(mb_detect_encoding($text, "UTF-8, ISO-8859-1", true)); var_dump(mb_detect_encoding("\xff\xfe", "UTF-8", true));

php encoding.php

string(5) "UTF-8"
bool(false)

The first list finds UTF-8 because it is named first and the bytes fit it. The two bytes \xff\xfe are not valid UTF-8, so the strict call answers false instead of guessing.

mb_convert_encoding

Normalize everything to UTF-8 right at the input boundary. Function mb_convert_encoding rewrites bytes from one encoding to another.

Convert once on the way in, then use only multibyte functions for length and slicing.

<?php $text = "caf\xE9"; $utf8 = mb_convert_encoding($text, "UTF-8", "ISO-8859-1"); echo mb_strlen($utf8, "UTF-8"), PHP_EOL; echo bin2hex($utf8), PHP_EOL;

php convert.php

4
636166c3a9

The source is a Latin-1 é written as the single byte e9; after the conversion the same character is the two bytes c3a9, which is UTF-8, and mb_strlen reads four characters.

Exercises

  1. Print the byte length and the character length of a string with three emoji.
  2. Cut the last two characters of a word with mb_substr and print them.
  3. Feed mb_detect_encoding a byte string that is valid Latin-1 but not UTF-8 and describe the result.
  4. Convert a UTF-8 word back to ISO-8859-1 and count its bytes again.
  5. Compare strrev() and mb_str_split() on an accented word.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does strlen() count?

    Answer

    Bytes

    strlen() is a byte function: a two-byte UTF-8 letter counts as two.

  2. What does the third argument of mb_substr() mean?

    Answer

    How many characters to keep

    The offset and the length of mb_substr() are counted in characters, not in bytes.

  3. What does the strict mode of mb_detect_encoding() return when no listed encoding fits?

    Answer

    false

    Strict mode refuses to guess: the call returns false when the bytes are invalid in every candidate.

  4. Where should text be converted to UTF-8?

    Answer

    Once at the input boundary

    One conversion on the way in keeps the rest of the code in a single encoding.

  5. What does this program print? <?php $text = "caf\u{E9}"; echo strlen($text);

    Answer

    5

    Three ASCII letters take one byte each and the accented letter takes two, so the byte count is five.

  6. What does this program print? <?php $text = "caf\u{E9}"; echo mb_strlen($text, "UTF-8");

    Answer

    4

    mb_strlen() counts characters, and the word has four of them.

  7. What does this program print? <?php $text = "h\u{E9}llo"; echo mb_substr($text, 1, 3, "UTF-8");

    Answer

    éll

    The offset 1 skips the h and the length 3 keeps the accented letter and the two l characters.

  8. What does this program print? <?php $text = "h\u{E9}llo"; echo mb_substr($text, 0, 2, "UTF-8");

    Answer

    hé

    The first two characters of the word are the h and the accented letter.

  9. What goes wrong when substr() cuts a multibyte string?

    Answer

    A character is split in half and the result is not valid UTF-8

    substr() counts bytes, so it can stop between the two bytes of one letter and leave a broken sequence.

  10. What does this program print? <?php var_dump(mb_detect_encoding("hello", "UTF-8", true));

    Answer

    string(5) "UTF-8"

    Plain ASCII bytes are also valid UTF-8, so the only listed candidate is the answer.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev