2015-11-02 58 views
1

我有以下函數來獲取一個語言字符串,它適用於英語而不是葡萄牙語字符。PHP - 數據庫結果編碼?

public function GetAllLanguageStrings($langid) 
    { 
     $GLOBALS['mysqli']->query("use " . $GLOBALS['db_name']); 
     $stmt = $GLOBALS['mysqli']->prepare("SELECT lang_identifier, text FROM lang_strings WHERE lang_id = ?"); 
     $stmt->bind_param("i", $langid); 
     $stmt->execute(); 
     $stmt->bind_result($identifier,$text); 
     $result = array("LangStrings" => array()); 

     while ($stmt->fetch()) { 
      array_push($result['LangStrings'], array("Identifier" => $identifier, "Text" => $text)); 
     } 

     return json_encode($result, JSON_PRETTY_PRINT); 
     $stmt->close(); 
    } 

結果變成是

{ "Identifier": "LOGIN_INFORMATION", "Text": "Informa\u00e7\u00f5es de Login." } 

代替

{ "Identifier": "LOGIN_INFORMATION", "Text": "Informações de Login." } 

我嘗試添加以下設置怪異:

$mysqli->query("SET NAMES utf8"); 
$mysqli->query("SET CHARACTER SET utf8"); 
$mysqli->set_charset("utf8"); 

,但什麼都沒有改變。

回答

1

JSON_UNESCAPED_UNICODE標誌可能是你正在尋找的。

有關json_encode如何輸出數據以及如何通過設置右標誌來操作輸出的詳細信息,請參閱this explanation

+0

更好: { 「標識」: 「LOGIN_INFORMATION」, 「文」: 「InformaçÃμes日登錄」}, – EchO

+0

以及爲文字存儲爲UTF8 IM的分貝? –

+0

看起來很像它:http://www.fileformat.info/info/unicode/char/00E7/index.htm你真正結束了正確的編碼值。網絡服務器是否返回utf8編碼的內容?檢查標題。文檔中是否指定了另一個字符集? –

0
return utf8_decode(json_encode($result, JSON_UNESCAPED_UNICODE)); 
比以前,但仍然