Home > Article > Backend Development > How to Resolve Encoding Mismatch Issues When URL Decoding in PHP?
URL Decoding in PHP: Encoding Mismatch Issue
When working with URL-encoded strings in PHP, there are occasional hiccups related to encoding conflicts. One such case arises when decoding a URL string that has both URL encoding and UTF-8 encoding.
Consider the following URL string:
Ant%C3%B4nio+Carlos+Jobim
This string is intended to decode to the following:
Antônio Carlos Jobim
However, when we use the urldecode() function, we obtain:
Antônio Carlos Jobim
This discrepancy occurs because the original string is not only URL-encoded but also UTF-8 encoded. To resolve this issue, we need to employ the utf8_decode() function, which converts UTF-8 encoding to Unicode.
The following code snippet demonstrates the solution:
<code class="php">echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));</code>
This will successfully output the expected string:
Antônio Carlos Jobim
The above is the detailed content of How to Resolve Encoding Mismatch Issues When URL Decoding in PHP?. For more information, please follow other related articles on the PHP Chinese website!