Home > Backend Development > PHP Tutorial > How to Decode UTF-8 Encoded URLs in PHP?

How to Decode UTF-8 Encoded URLs in PHP?

Patricia Arquette
Release: 2024-10-17 15:04:02
Original
607 people have browsed it

How to Decode UTF-8 Encoded URLs in PHP?

Decoding Encoded URLs with PHP

In PHP, the urldecode function is used to decode URL-encoded strings. However, a common issue arises when a URL contains UTF-8 encoded characters.

The Problem:

Attempting to decode a URL with an encoded UTF-8 character, such as ô, using urldecode may result in an incorrect output, despite the string decoding correctly in online JS-based decoders.

The Solution:

To properly decode a URL with UTF-8 encoded characters in PHP, it is necessary to utilize the utf8_decode function in conjunction with urldecode. The process includes:

  1. First, decode the URL using urldecode:
$url_decoded = urldecode("Ant%C3%B4nio+Carlos+Jobim");
Copy after login
  1. Next, decode the UTF-8 encoded characters using utf8_decode:
$utf8_decoded = utf8_decode($url_decoded);
Copy after login

Combining these steps ensures that the URL is correctly decoded and the UTF-8 characters are interpreted corretamente. Output:

'Antônio Carlos Jobim'
Copy after login

The above is the detailed content of How to Decode UTF-8 Encoded URLs 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