Home >Backend Development >PHP Tutorial >How to Resolve Encoding Mismatch Issues When URL Decoding in PHP?

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-17 15:09:03562browse

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!

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