1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

better than before but still: {"Identifier":"LOGIN_INFORMATION","Text":"Informações de Login."},
well is the text stored as utf8 im the db?
look slike it: fileformat.info/info/unicode/char/00E7/index.htm you actualy end up with the correct encoded value. Does the webserver return utf8 encoded content? check the headers. Is there another charset specified in the document?
The solution: return utf8_decode(json_encode($result, JSON_UNESCAPED_UNICODE)); still i'm marking your answer as correct because you helped out ;)
0
return utf8_decode(json_encode($result, JSON_UNESCAPED_UNICODE));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.