如何在 PHP 中解碼 UTF-8 編碼的 URL?

Patricia Arquette
發布: 2024-10-17 15:04:02
原創
512 人瀏覽過

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'
登入後複製

以上是如何在 PHP 中解碼 UTF-8 編碼的 URL?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!