Function name: mb_check_encoding()
Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7
Function description: mb_check_encoding() function is used to check whether a string is the specified character encoding.
Usage: bool mb_check_encoding ( string $var [, string $encoding = mb_internal_encoding() ] )
parameter:
- $var: The string to be checked.
- $encoding (optional): The character encoding to check. If not specified, the current internal character encoding is used.
Return value:
- Return true if the string $var is the specified character encoding $encoding.
- If the string $var is not the specified character encoding $encoding, false is returned.
Example:
// 检查字符串是否是UTF-8 编码$str = "Hello, 世界!"; if (mb_check_encoding($str, "UTF-8")) { echo "字符串是UTF-8 编码"; } else { echo "字符串不是UTF-8 编码"; }
Output:
字符串是UTF-8 编码
Notes:
- This function relies on the mbstring extension and needs to be enabled in the PHP configuration file.
- If no character encoding $encoding is specified, the function will check using the current internal character encoding returned by mb_internal_encoding().
- If you need to check multiple character encodings, you can use the mb_detect_encoding() function.