Home > Backend Development > PHP Tutorial > How to Handle Unicode Characters in URL Decoding for PHP?

How to Handle Unicode Characters in URL Decoding for PHP?

Mary-Kate Olsen
Release: 2024-10-17 15:07:02
Original
482 people have browsed it

How to Handle Unicode Characters in URL Decoding for PHP?

Troubleshooting URL Decoding in PHP

URL decoding in PHP using the urldecode function may return unexpected results when dealing with Unicode characters. To decode a URL string containing UTF-8 encoded characters, we need to combine urldecode with utf8_decode.

Example:

Consider the URL string:

Ant%C3%B4nio+Carlos+Jobim
Copy after login

This string represents the Unicode character Antônio. When we try to decode it using urldecode only:

urldecode("Ant%C3%B4nio+Carlos+Jobim");
Copy after login

It results in:

Antônio Carlos Jobim
Copy after login

To fix this, we first need to decode the URL encoding and then the UTF-8 encoding:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));
Copy after login

This will correctly output the expected string:

Antônio Carlos Jobim
Copy after login

Remember, when dealing with URLs that contain Unicode characters, it's essential to use both urldecode and utf8_decode to ensure proper decoding.

The above is the detailed content of How to Handle Unicode Characters in URL Decoding for 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