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.

EchO
  • 1,274
  • 1
  • 10
  • 17

2 Answers2

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.

Community
  • 1
  • 1
Stefan Dochow
  • 1,454
  • 1
  • 10
  • 11
  • better than before but still: {"Identifier":"LOGIN_INFORMATION","Text":"Informações de Login."}, – EchO Nov 02 '15 at 20:15
  • well is the text stored as utf8 im the db? – Stefan Dochow Nov 02 '15 at 20:28
  • look slike it: http://www.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? – Stefan Dochow Nov 02 '15 at 20:30
  • The solution: return utf8_decode(json_encode($result, JSON_UNESCAPED_UNICODE)); still i'm marking your answer as correct because you helped out ;) – EchO Nov 02 '15 at 20:31
0
return utf8_decode(json_encode($result, JSON_UNESCAPED_UNICODE));
TRiG
  • 10,148
  • 7
  • 57
  • 107
EchO
  • 1,274
  • 1
  • 10
  • 17