When performing web encoding detection or processing, PHP's mb_get_info() function can provide information about the current multibyte string setting, which is very useful. However, during actual use, developers often encounter some problems. This article summarizes common errors and solutions when using mb_get_info() to check web encoding.
Error description: Call mb_get_info() directly, but the system prompts that the function cannot be found.
Cause analysis: mb_get_info() belongs to the mbstring extension. If PHP does not install or enable mbstring , an error will occur when calling the related functions.
Workaround: Make sure the mbstring extension is enabled in the php.ini file. For example:
extension=mbstring
If the server is not installed, you can refer to the following command to install it (taking Ubuntu as an example):
sudo apt-get install php-mbstring
sudo service apache2 restart
After installation, visit your test page, for example:
https://gitbox.net/test_mb.php
Confirm that mb_get_info() works normally.
Error description: I hope mb_get_info() will directly return the encoding information of the web page content, and the result is a configuration array.
Cause analysis: mb_get_info() returns the current status of the PHP mbstring configuration (such as internal encoding, language settings, etc.), rather than directly telling you the actual encoding of the web page .
Workaround: If you want to detect the actual encoding of the web page content, you should use mb_detect_encoding() instead of mb_get_info() .
Example:
$content = file_get_contents('https://gitbox.net/page.html');
$encoding = mb_detect_encoding($content, mb_list_encodings(), true);
echo "The web encoding may be:$encoding";
Error description: Although mb_get_info() is used to check internal encoding, garbled code still occurs when processing multibyte strings.
Cause analysis: The default internal encoding may not be UTF-8, causing functions such as mb_strlen() and mb_substr() to return exceptions.
Solution: Before processing, actively set the correct internal encoding, such as:
mb_internal_encoding('UTF-8');
Then call mb_get_info() to check whether the setting is successful:
print_r(mb_get_info());
Error description: No parameters were passed when calling mb_get_info() , and the information obtained was not enough.
Cause analysis: mb_get_info() allows passing in a string parameter (such as "http_input" , "http_output" , "internal_encoding" , etc.) to query specific items. If the parameters are not passed, all information will be returned.
Solution: If you only want to check a certain item, such as internal encoding, you can write it like this:
$encoding = mb_get_info("internal_encoding");
echo "The current internal code is:$encoding";
Access result example:
https://gitbox.net/show_encoding.php
When checking web encoding using mb_get_info() , remember:
Make sure the mbstring extension is enabled;
Understand that it is query setting status, not detecting web page encoding;
Correctly set and check internal encoding;
Pass appropriate parameters according to the requirements.
If you need to actually detect the actual encoding of the web page, please give priority to using mb_detect_encoding() with the method of reading content.