While there are many languages in which every necessary character can be represented by a one-to-one mapping to an 8-bit value, there are also several languages which require so many characters for written communication that they cannot be contained within the range a mere byte can code (A byte is made up of eight bits. Each bit can contain only two distinct values, one or zero. Because of this, a byte can only represent 256 unique values (two to the power of eight)). Multibyte character encoding schemes were developed to express more than 256 characters in the regular bytewise coding system.
When you manipulate (trim, split, splice, etc.) strings encoded in a multibyte encoding, you need to use special functions since two or more consecutive bytes may represent a single character in such encoding schemes. Otherwise, if you apply a non-multibyte-aware string function to the string, it probably fails to detect the beginning or ending of the multibyte character and ends up with a corrupted garbage string that most likely loses its original meaning. Use mb_split() instead. Note that there are no mb_trim() or mb_splice() functions defined in PHP language. What does that mean? Well … do not use them for non ASCII string trimming. Use alternative functions like mb_ereg_replace() using the right regex. It is always good idea to check the encoding before performing string manipulation. To check mb_check_encoding(); Also read more about how to use regex here.
mbstring
provides multibyte specific string functions that help you deal with multibyte encodings in PHP. In addition to that, mbstring
handles character encoding conversion between the possible encoding pairs. mbstring
is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience (listed below).
mb_ functions:
- mb_check_encoding — Check if strings are valid for the specified encoding
- mb_chr — Return character by Unicode code point value
- mb_convert_case — Perform case folding on a string
- mb_convert_encoding — Convert character encoding
- mb_convert_kana — Convert “kana” one from another (“zen-kaku”, “han-kaku” and more)
- mb_convert_variables — Convert character code in variable(s)
- mb_decode_mimeheader — Decode string in MIME header field
- mb_decode_numericentity — Decode HTML numeric string reference to character
- mb_detect_encoding — Detect character encoding
- mb_detect_order — Set/Get character encoding detection order
- mb_encode_mimeheader — Encode string for MIME header
- mb_encode_numericentity — Encode character to HTML numeric string reference
- mb_encoding_aliases — Get aliases of a known encoding type
- mb_ereg_match — Regular expression match for multibyte string
- mb_ereg_replace_callback — Perform a regular expression search and replace with multibyte support using a callback
- mb_ereg_replace — Replace regular expression with multibyte support
- mb_ereg_search_getpos — Returns start point for next regular expression match
- mb_ereg_search_getregs — Retrieve the result from the last multibyte regular expression match
- mb_ereg_search_init — Setup string and regular expression for a multibyte regular expression match
- mb_ereg_search_pos — Returns position and length of a matched part of the multibyte regular expression for a predefined multibyte string
- mb_ereg_search_regs — Returns the matched part of a multibyte regular expression
- mb_ereg_search_setpos — Set start point of next regular expression match
- mb_ereg_search — Multibyte regular expression match for predefined multibyte string
- mb_ereg — Regular expression match with multibyte support
- mb_eregi_replace — Replace regular expression with multibyte support ignoring case
- mb_eregi — Regular expression match ignoring case with multibyte support
- mb_get_info — Get internal settings of mbstring
- mb_http_input — Detect HTTP input character encoding
- mb_http_output — Set/Get HTTP output character encoding
- mb_internal_encoding — Set/Get internal character encoding
- mb_language — Set/Get current language
- mb_list_encodings — Returns an array of all supported encodings
- mb_ord — Get Unicode code point of character
- mb_output_handler — Callback function converts character encoding in output buffer
- mb_parse_str — Parse GET/POST/COOKIE data and set global variable
- mb_preferred_mime_name — Get MIME charset string
- mb_regex_encoding — Set/Get character encoding for multibyte regex
- mb_regex_set_options — Set/Get the default options for mbregex functions
- mb_scrub — Description
- mb_send_mail — Send encoded mail
- mb_split — Split multibyte string using regular expression
- mb_str_split — Given a multibyte string, return an array of its characters
- mb_strcut — Get part of string
- mb_strimwidth — Get truncated string with specified width
- mb_stripos — Finds position of first occurrence of a string within another, case insensitive
- mb_stristr — Finds first occurrence of a string within another, case insensitive
- mb_strlen — Get string length
- mb_strpos — Find position of first occurrence of string in a string
- mb_strrchr — Finds the last occurrence of a character in a string within another
- mb_strrichr — Finds the last occurrence of a character in a string within another, case insensitive
- mb_strripos — Finds position of last occurrence of a string within another, case insensitive
- mb_strrpos — Find position of last occurrence of a string in a string
- mb_strstr — Finds first occurrence of a string within another
- mb_strtolower — Make a string lowercase
- mb_strtoupper — Make a string uppercase
- mb_strwidth — Return width of string
- mb_substitute_character — Set/Get substitution character
- mb_substr_count — Count the number of substring occurrences
- mb_substr — Get part of string
Se also PHP mb_ CONSTANTS
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
- MB_OVERLOAD_MAIL (int) – Removed as of PHP 8.0.0.
- MB_OVERLOAD_STRING (int) – Removed as of PHP 8.0.0.
- MB_OVERLOAD_REGEX (int) – Removed as of PHP 8.0.0.
- MB_CASE_UPPER (int) – Performs a full upper-case folding. This may change the length of the string. This is the mode used by mb_strtoupper().
- MB_CASE_LOWER (int) – Performs a full lower-case folding. This may change the length of the string. This is the mode used by mb_strtolower().
- MB_CASE_TITLE (int) – Performs a full title-case conversion based on the Cased and CaseIgnorable derived Unicode properties. In particular this improves handling of quotes and apostrophes. This may change the length of the string.
- MB_CASE_FOLD (int) – Performs a full case fold conversion which removes case distinctions present in the string. This is used for caseless matching. This may change the length of the string. Available since PHP 7.3.
- MB_CASE_LOWER_SIMPLE (int) – Performs a simple lower-case fold conversion. This does not change the length of the string. Available as of PHP 7.3.
- MB_CASE_UPPER_SIMPLE (int) – Performs simple upper-case fold conversion. This does not change the length of the string. Available as of PHP 7.3.
- MB_CASE_TITLE_SIMPLE (int) – Performs simple title-case fold conversion. This does not change the length of the string. Available as of PHP 7.3.
- MB_CASE_FOLD_SIMPLE (int) – Performs a simple case fold conversion which removes case distinctions present in the string. This is used for caseless matching. This does not change the length of the string. Used by case-insensitive operations internally by the MBString extension. Available as of PHP 7.3.
- MB_ONIGURUMA_VERSION (string) – The Oniguruma version, e.g. 6.9.4. Available as of PHP 7.4.
Read src:
Mbstring in PHP Docs: https://www.php.net/manual/en/book.mbstring.php
Overloading no mb_ (before PHP 7.2 – only ): https://www.php.net/manual/en/mbstring.overload.php
Supported encodings: https://www.php.net/manual/en/mbstring.supported-encodings.php