Skip to main content

Module mb

Module mb 

Source
Expand description

Multibyte (mbstring) functions on UTF-8.

Ported from PHP 8.5.11’s ext/mbstring for the UTF-8 encoding (PHP’s default internal encoding), including its handling of invalid UTF-8: the decoder turns each invalid sequence into one error marker (one “character” for mb_strlen), and functions that re-encode text write ? (mbstring.substitute_character’s default) for it. Some functions work on bytes and never re-encode (mb_strpos); each function’s docs say which.

Lower-casing uses PHP’s own Unicode tables ([unicode], generated from unicode_data.h), with PHP’s final sigma rule.

PHPRust
mb_strlenmb_strlen, mb_strlen_in with Encoding ('8bit')
mb_substrmb_substr
mb_strtolowermb_strtolower
mb_strposmb_strpos
mb_check_encoding, mb_scrub, mb_convert_encoding($s, 'UTF-8')mb_check_encoding, mb_scrub
mb_strimwidth($s, 0, $width)mb_strimwidth

§Unicode data

mb/unicode.rs is generated by data/unicode.py from php-src’s ext/mbstring/unicode_data.h at tag php-8.5.11: the lower case table (_uccase_lower_table, sorted by code point), _uccase_extra_table, and the UC_CASED and UC_CASE_IGNORABLE ranges of _ucprop_ranges. Regenerate it when PHP updates its Unicode version.

Enums§

Encoding
An encoding mb_strlen accepts.

Functions§

mb_check_encoding
PHP mb_check_encoding($value, 'UTF-8') on a string: strict UTF-8 (no overlong forms, surrogates or code points above U+10FFFF).
mb_scrub
PHP mb_scrub($string) and mb_convert_encoding($string, 'UTF-8') from UTF-8: each invalid sequence becomes ?. Borrows valid input.
mb_strimwidth
PHP mb_strimwidth($string, 0, $width) (no trim marker): the longest leading part of the string that fits in width columns. A string that fits is returned as it is, except that invalid sequences become ?, as they do in a cut one.
mb_strlen
PHP mb_strlen($string): the number of characters; each invalid sequence counts as one.
mb_strlen_in
PHP mb_strlen($string, $encoding) for the encodings in Encoding.
mb_strpos
PHP mb_strpos($haystack, $needle, $offset): character position of the first occurrence at or after offset. Matches bytes; positions count lead bytes. None is PHP’s false.
mb_strtolower
PHP mb_strtolower($string): full lower case with the final sigma rule (MB_CASE_LOWER).
mb_substr
PHP mb_substr($string, $start, $length): characters from start (negative: from the end) for length characters (negative: stop that many before the end; None: to the end). Re-encodes: invalid sequences in the result become ?. PHP_INT_MIN for either argument is PHP’s ValueError.