Heim  >  Artikel  >  Backend-Entwicklung  >  Wie dekodiere ich UTF-8-kodierte URLs in PHP?

Wie dekodiere ich UTF-8-kodierte URLs in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-17 15:04:02514Durchsuche

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 %C3%B4, 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");
  1. Next, decode the UTF-8 encoded characters using utf8_decode:
$utf8_decoded = utf8_decode($url_decoded);

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

'Antônio Carlos Jobim'

Das obige ist der detaillierte Inhalt vonWie dekodiere ich UTF-8-kodierte URLs in PHP?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn