无效的编码json(显示西里尔字母时出现问题)

时间:2019-06-18 06:07:45

标签: php json

我正在尝试从表单推断文件,显示西里尔字母时出现问题

Php(7.1)

<?php
    header("Content-type: application/json; charset=utf-8");
    file_put_contents('export.json', json_encode($_REQUEST) );
?>

输出 You_name字段用字符写。

{"Entry_ID":"71","You_name":"\u041a\u043e\u0437\u043b\u0430\u043d\u0431\u0435\u043a \u0410\u043c\u0438\u0440 \u041f\u043e\u043c\u0438\u0434\u043e\u0440\u043e\u0432\u0438\u0447","You_phone":"7 (902) 998 1019","You_date":"19\/06\/2019"}

3 个答案:

答案 0 :(得分:0)

您应该使用$ _POST来获取如下所示的表单数据

  header("Content-type: application/json; charset=utf-8");

  file_put_contents('export.json', json_encode($_POST) );

答案 1 :(得分:0)

尝试一下(source):

(loop :for divisor :of-type fixnum :from start :to stop
      :always (divides-even-p num divisor))

答案 2 :(得分:0)

6岁的答案仍然有效:Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?

json_encode( $text, JSON_UNESCAPED_UNICODE );

示例:

/tmp $ cat test.php
<?php
echo json_encode(['key'=>'ыфва']), PHP_EOL;
echo json_encode(['key'=>'ыфва'], JSON_UNESCAPED_UNICODE), PHP_EOL;
/tmp $ php test.php
{"key":"\u044b\u0444\u0432\u0430"}
{"key":"ыфва"}
/tmp $ php --version
PHP 7.3.6 (cli) (built: May 31 2019 23:38:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
    with Zend OPcache v7.3.6, Copyright (c) 1999-2018, by Zend Technologies