How to Resolve Encoding Mismatch Issues When URL Decoding in PHP?

Mary-Kate Olsen
Release: 2024-10-17 15:09:03
Original
444 people have browsed it

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

This string is intended to decode to the following:

Antônio Carlos Jobim
Copy after login
Copy after login

However, when we use the urldecode() function, we obtain:

Antônio Carlos Jobim
Copy after login

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

This will successfully output the expected string:

Antônio Carlos Jobim
Copy after login
Copy after login

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!

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!