Current Location: Home> Latest Articles> How to Correctly Handle Multibyte Characters Using mb_convert_encoding and html_entity_decode?

How to Correctly Handle Multibyte Characters Using mb_convert_encoding and html_entity_decode?

gitbox 2025-06-15

In PHP development, we often encounter situations where we need to handle multibyte character sets (such as Chinese, Japanese, Korean, etc.). In such cases, mb_convert_encoding and html_entity_decode are two very useful functions that help us correctly handle character encoding and HTML entities. This article will explore in detail how to use these two functions together to handle multibyte characters correctly.

What are mb_convert_encoding and html_entity_decode?

  1. mb_convert_encoding:
    This function is a multibyte string handling function in PHP, primarily used for converting character encodings between different character sets. For multibyte character sets (such as UTF-8, GBK, etc.), mb_convert_encoding is effective for conversion.

    Example usage:

    $str = mb_convert_encoding($str, 'UTF-8', 'GBK');
    

    The code above converts $str from GBK encoding to UTF-8 encoding.

  2. html_entity_decode:
    This function is used to convert HTML entities (such as <, >, &) back to their corresponding characters. This function is very useful when processing HTML content, especially when the HTML content has been entity encoded and we want to restore the original characters.

    Example usage:

    $str = html_entity_decode($str, ENT_QUOTES, &#039;UTF-8&#039;);
    

Using mb_convert_encoding and html_entity_decode Together to Handle Multibyte Characters

When dealing with HTML content that contains multibyte characters, we may encounter the following two situations:

  1. Content has been HTML entity encoded: The characters in the HTML content might have been converted into entity form (for example, < replaced by <). In this case, we need to use html_entity_decode first to decode the entities back to normal characters.

  2. Inconsistent character encoding: In some scenarios, the character encoding of the HTML content might be different from the default encoding used by PHP (for example, the HTML content might be UTF-8 encoded, while the PHP program is using GBK encoding). To avoid garbled text issues, we can use mb_convert_encoding to convert the content into the appropriate encoding.

Practical Example

Let's assume we retrieve some HTML content from the database that contains Chinese characters, and these characters have been HTML entity encoded. To display these characters correctly, we can follow these steps:

  1. Use mb_convert_encoding to ensure the character encoding of the HTML content matches the current PHP program.

  2. Use html_entity_decode to convert the HTML entities back to normal characters.

Here is a complete code example:

<?php
// Assume HTML content retrieved from the database
$html_content = "&lt;div&gt;你好,世界!&lt;/div&gt;";
<p>// Step 1: Convert encoding from GBK to UTF-8<br>
$html_content = mb_convert_encoding($html_content, 'UTF-8', 'GBK');</p>
<p>// Step 2: Decode HTML entities to normal characters<br>
$html_content = html_entity_decode($html_content, ENT_QUOTES, 'UTF-8');</p>
<p>echo $html_content;  // Output: <div>你好,世界!</div><br>
?><br>

In the code above, mb_convert_encoding first converts the HTML content from GBK encoding to UTF-8 encoding, and then html_entity_decode decodes the HTML entities. The final output is the correct HTML format, with the Chinese characters displayed properly.

Common Issues and Solutions

  1. Garbled text issues: If the output content is still garbled, it could be due to inconsistent PHP default encoding settings. You can set the default encoding using mb_internal_encoding and mb_http_output functions:

    mb_internal_encoding(&#039;UTF-8&#039;);
    mb_http_output(&#039;UTF-8&#039;);
    
  2. HTML entities not decoded properly: If html_entity_decode cannot decode certain special characters, it could be because the ENT_QUOTES parameter was not specified correctly. You can try changing the parameter to ENT_NOQUOTES or another suitable option.

  3. Encoding issues in URLs: If the HTML content contains URLs, and the character encoding of the URL differs from the page encoding, URL errors might occur. In this case, you can use urlencode and urldecode to handle URL encoding:

    $url = "http://gitbox.net/somepage?param=" . urlencode("你好,世界!");
    

Conclusion

Using mb_convert_encoding and html_entity_decode together can effectively solve issues related to multibyte character encoding and HTML entity decoding. In real-world development, we often encounter inconsistent encoding or HTML entity encoding issues, and these two functions can help us handle them easily. Mastering the usage of these functions can improve the stability and reliability when handling multibyte characters.