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.
| PHP | Rust |
|---|---|
mb_strlen | mb_strlen, mb_strlen_in with Encoding ('8bit') |
mb_substr | mb_substr |
mb_strtolower | mb_strtolower |
mb_strpos | mb_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_strlenaccepts.
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)andmb_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 inwidthcolumns. 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 inEncoding. - mb_
strpos - PHP
mb_strpos($haystack, $needle, $offset): character position of the first occurrence at or afteroffset. Matches bytes; positions count lead bytes.Noneis PHP’sfalse. - 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 fromstart(negative: from the end) forlengthcharacters (negative: stop that many before the end;None: to the end). Re-encodes: invalid sequences in the result become?.PHP_INT_MINfor either argument is PHP’sValueError.