Home  >  Article  >  Backend Development  >  How to Decode UTF-8 Encoded URLs Beyond urldecode() in PHP?

How to Decode UTF-8 Encoded URLs Beyond urldecode() in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-17 15:08:02157browse

How to Decode UTF-8 Encoded URLs Beyond urldecode() in PHP?

URL Decoding in PHP: Beyond urldecode()

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn