简体   繁体   中英

PHP - Database results encoding?

i have the following function to obtain a language strings, it works fine for english but not for portuguese characters.

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();
    }

the results turn out to be wierd

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

instead of

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

i tried to add the following settings:

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

but nothing changed.

The JSON_UNESCAPED_UNICODE flag might be what you are looking for.

Please refer to this explanation for details on how json_encode outputs data and how to manipulate the output by setting the right flags.

return utf8_decode(json_encode($result, JSON_UNESCAPED_UNICODE));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM