本文介紹了為什么PHP json_encode 函數會將UTF-8 字符串轉換為十六進制實體?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個處理多種語言的 PHP 腳本.不幸的是,每當我嘗試使用 json_encode
時,任何 Unicode 輸出都會轉換為十六進制實體.這是預期的行為嗎?有沒有辦法將輸出轉換為UTF-8字符?
I have a PHP script that deals with a wide variety of languages. Unfortunately, whenever I try to use json_encode
, any Unicode output is converted to hexadecimal entities. Is this the expected behavior? Is there any way to convert the output to UTF-8 characters?
以下是我所看到的示例:
Here's an example of what I'm seeing:
輸入
echo $text;
輸出
База данни грешка.
輸入
json_encode($text);
輸出
"u0411u0430u0437u0430 u0434u0430u043du043du0438 u0433u0440u0435u0448u043au0430."
推薦答案
自 PHP/5.4.0 起,有一個名為 JSON_UNESCAPED_UNICODE
的選項.看看:
Since PHP/5.4.0, there is an option called JSON_UNESCAPED_UNICODE
. Check it out:
https://php.net/function.json-encode
因此您應該嘗試:
json_encode( $text, JSON_UNESCAPED_UNICODE );
這篇關于為什么PHP json_encode 函數會將UTF-8 字符串轉換為十六進制實體?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!