PHP's urldecode() function is a valuable tool for decoding URL-encoded strings. However, it can fall short when the string is also UTF-8 encoded.
The Problem:
Consider the following URL-encoded string:
Ant%C3%B4nio+Carlos+Jobim
Attempting to decode it using urldecode() results in the following output:
Antônio Carlos Jobim
Instead of the expected "Antônio Carlos Jobim."
The Solution:
To resolve this issue, we must decode the UTF-8 encoding as well. PHP's utf8_decode() function serves this purpose:
echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));
This will output the correct string:
Antônio Carlos Jobim
Explanation:
URL encoding involves replacing certain characters with their hexadecimal counterparts. UTF-8 encoding, on the other hand, represents characters using multiple bytes, which can be misinterpreted as hex codes. By combining utf8_decode() and urldecode(), we can correctly decode strings that have been encoded in both UTF-8 and URL formats.
The above is the detailed content of How to Decode UTF-8 Encoded URLs Beyond urldecode() in PHP?. For more information, please follow other related articles on the PHP Chinese website!