PHP strings are byte arrays with no built-in notion of characters, so non-ASCII text such as Hindi, Chinese or emoji needs the mbstring extension. The multibyte family, the mb_ functions, treats a string as UTF-8 characters, so mb_strlen("नमस्ते") counts characters, not bytes. This guide shows how php mb_ functions unicode handling works, what goes wrong with the plain functions, and how to design a safe UTF-8 pipeline in PHP 8.x.
In UTF-8, a character uses between one and four bytes. The classic string functions count and slice bytes, which cuts multibyte characters in half and produces invalid output.
The broken substr() result may display as a replacement symbol or fail when passed to json_encode(), which returns false on invalid UTF-8 unless you pass JSON_INVALID_UTF8_SUBSTITUTE or JSON_THROW_ON_ERROR to find the problem. The same issue hits strtoupper(), ucfirst(), str_pad(), wordwrap() and strrev().
The extension is bundled with PHP but may need enabling: check extension_loaded('mbstring') or php -m. These functions are the multibyte counterparts you will use most.
| Byte-based | Multibyte | Notes |
|---|---|---|
strlen() | mb_strlen() | character count |
substr() | mb_substr() | character offsets |
strpos() | mb_strpos() | character position |
strtoupper() | mb_strtoupper() | handles Unicode case rules |
str_split() | mb_str_split() | PHP 7.4 and later |
str_pad() | mb_str_pad() | added in PHP 8.3 |
trim() | mb_trim() | added in PHP 8.4 |
ucfirst() | mb_ucfirst() | added in PHP 8.4 |
On PHP versions older than 8.3 or 8.4, mb_str_pad(), mb_trim() and mb_ucfirst() do not exist, so check your version or write a small helper.
Every mb_ function takes an optional encoding argument. If you leave it out, PHP uses the internal encoding, which defaults to the value of default_charset, and that is UTF-8 since PHP 5.6. Setting it explicitly in a bootstrap file makes the intent clear.
Use mb_check_encoding() to validate input and mb_convert_encoding() to convert from a known source encoding. Avoid mb_detect_encoding() for important decisions, because detection is a guess; with strict mode enabled it is more reliable, but a known encoding is always better.
A character on screen is not always one code point. The letter é can be one code point (U+00E9) or e followed by a combining accent (U+0065 U+0301). Emoji with skin tones and many Indic scripts also combine several code points into one visible unit.
The mb_ functions count code points. For user-visible characters use the grapheme_* functions from the intl extension. To compare text that may use different forms, normalise first with the Normalizer class: Normalizer::normalize($s, Normalizer::FORM_C).
Unicode case rules are not one-to-one: the German ß uppercases to SS, and the Turkish dotted İ is special. mb_strtoupper('straße') returns STRASSE on PHP 7.3 and later. For case-insensitive equality, use mb_strtolower() on both sides, or the more accurate mb_convert_case($s, MB_CASE_FOLD). For locale-aware sorting, use Collator from intl, because strcmp() compares raw bytes.
Regular expressions need the u modifier to treat the subject as UTF-8 and to enable Unicode properties.
Without u, . matches one byte and \w ignores accented letters. A pattern with u returns false for invalid UTF-8, so check preg_last_error_msg().
Content-Type: text/html; charset=UTF-8 and add <meta charset="utf-8"> in the page.utf8mb4 character set for MySQL connections and tables, not utf8, which stores only three bytes per character.mb_check_encoding() or preg_match('//u', $s).mb_ functions for length limits, truncation and padding.htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8').For storing and printing UTF-8, no, because PHP passes bytes through unchanged. You need mbstring as soon as you count, cut, pad or change the case of characters.
strlen() returns bytes and mb_strlen() returns code points in the given encoding. They agree only for ASCII text.
Usually a layer is not UTF-8: the file, the HTTP header, the database connection or the column. Fix the layer that uses a different charset, for example set utf8mb4 on the connection.
Use mb_substr($s, 0, $n) for code points, or mb_strimwidth($s, 0, $n, '...') to limit display width, which counts full-width East Asian characters as two.
PHP strings are bytes, so use the mb_ functions whenever text may contain non-ASCII characters. Set UTF-8 everywhere in the pipeline, validate input with mb_check_encoding(), add the u flag to regular expressions, normalise with Normalizer, and use intl grapheme functions when you need user-visible characters. Check your PHP version before using mb_str_pad(), mb_trim() and mb_ucfirst().