Example 1 — Count Characters
Compare mb_strlen() with strlen().
<?php
$text = "Héllo";
echo strlen($text), PHP_EOL;
echo mb_strlen($text, "UTF-8"), PHP_EOL; How It Works
- Use a word with an accent. The letter
éis 2 bytes. - strlen() counts bytes, so it returns 6.
- mb_strlen() counts characters, so it returns 5.
- Pass
UTF-8so the result does not depend on settings.

