Skip to content
Tutorials Center
My learning

Topics

PHP Reference · String Functions

PHP multibyte string functions (mb_*)

Overview of PHP's mbstring functions: why strlen() miscounts UTF-8, the mb_* equivalents, encoding detection and conversion, and the newer mb_trim and mb_str_pad.

A PHP string is a sequence of bytes. Functions such as strlen(), substr() and strtoupper() work on those bytes, which is correct for ASCII but wrong for UTF-8 text, where one character can take two, three or four bytes. The mbstring extension adds mb_* functions that understand characters in a given encoding, so mb_strlen("é") is 1 while strlen("é") is 2.

Most modern PHP builds include mbstring, and the default encoding is UTF-8, so you rarely have to name an encoding. Use the mb_* function whenever text may contain anything beyond plain English letters. Even they work on code points, not on what a person sees as one character (a flag or a family emoji is several code points), for which the intl extension's grapheme functions are the tool.

PHP
<?php
echo strlen("naïve");      // 6 (bytes)
echo mb_strlen("naïve");   // 5 (characters)

Try it

PHP Byte functions versus mb_* functions

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Encodings: check, detect, convert, pad, trim

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The main functions

Byte function Multibyte version Notes
strlen mb_strlen Counts characters.
substr mb_substr Positions count characters.
strpos, stripos, strrpos mb_strpos, mb_stripos, mb_strrpos Return character offsets.
substr_count mb_substr_count
strtolower, strtoupper mb_strtolower, mb_strtoupper Full Unicode case mapping, including ß to SS.
ucfirst, lcfirst, ucwords mb_convert_case, mb_ucfirst, mb_lcfirst mb_ucfirst and mb_lcfirst arrived in PHP 8.4.
trim, ltrim, rtrim mb_trim, mb_ltrim, mb_rtrim PHP 8.4.
str_pad mb_str_pad PHP 8.3.
str_split mb_str_split
wordwrap none Use mb_strimwidth() to cut to a display width.
strrev none Reverse with implode("", array_reverse(mb_str_split($s))).

Encoding helpers: mb_internal_encoding(), mb_check_encoding(), mb_detect_encoding(), mb_convert_encoding(), mb_scrub() (replace invalid bytes), mb_strwidth(), mb_strimwidth() and mb_substitute_character().

Usage notes

  • Don't mix up bytes and characters. Offsets from strpos() must be used with substr(), and offsets from mb_strpos() with mb_substr().
  • Pass the encoding explicitly to mb_convert_encoding(), and in libraries that cannot rely on the global setting. The default for the other functions is mb_internal_encoding(), which is UTF-8 unless configured otherwise.
  • mb_detect_encoding() guesses. Always pass an explicit candidate list and true for strict mode, and prefer knowing the encoding from the source.
  • Validate input with mb_check_encoding($s, "UTF-8") before storing it. Invalid bytes can break JSON encoding and databases.
  • Regular expressions need the u modifier to treat text as UTF-8: preg_match('/\p{L}+/u', $s).
  • Not everything is covered. There is no mb_sprintf, mb_strrev or mb_ucwords; use mb_convert_case($s, MB_CASE_TITLE) for title case.
  • Performance. mb_* functions scan the string to find character positions, so they cost more than the byte versions on long strings. Use byte functions for ASCII-only data such as hex or Base64.