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

Susan Sarandon
Release: 2024-10-17 15:08:02
Original
156 people have browsed it

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
Copy after login

Attempting to decode it using urldecode() results in the following output:

Antônio Carlos Jobim
Copy after login

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"));
Copy after login

This will output the correct string:

Antônio Carlos Jobim
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!