PHP 5.3.3中的json_encode UTF-8

时间:2023-01-05 22:13:09

Trying to save data with specials characters using json_encode.

尝试使用json_encode使用特殊字符保存数据。

Code sample:

$a = array("Name" => "SáENZ PEñA");
echo "Normal : ",  json_encode($a), "<br>";
echo "Unicode : ", json_encode($a, JSON_UNESCAPED_UNICODE), "<br>";

JSON_UNESCAPED_UNICODE solve the issue with php 5.6

JSON_UNESCAPED_UNICODE用php 5.6解决了这个问题

Normal : {"Name":"S\u00e1ENZ PE\u00f1A"}
Unicode : {"Name":"SáENZ PEñA"}

but unfortunelly i have php 5.3 :

但不幸的是我有PHP 5.3:

Normal : {"Name":"S\u00e1ENZ PE\u00f1A"}
Unicode : 
Warning: json_encode() expects parameter 2 to be long, string given in /var/www/.../TEST.php on line 4

Any solution that works with php 5.3 ?

任何适用于php 5.3的解决方案?

1 个解决方案

#1


1  

If you can't use JSON_UNESCAPED_UNICODE, you could probably unescape the JSON yourself after it's been encoded:

如果你不能使用JSON_UNESCAPED_UNICODE,你可能会在编码后自己忘记JSON:

  • Compatible with \ (escaped backslashes itself)
  • 兼容\(转义反斜杠本身)

  • Compatible with JSON_HEX_* flags

    与JSON_HEX_ *标志兼容

    function raw_json_encode($input, $flags = 0) { $fails = implode('|', array_filter(array( '\\\\', $flags & JSON_HEX_TAG ? 'u003[CE]' : '', $flags & JSON_HEX_AMP ? 'u0026' : '', $flags & JSON_HEX_APOS ? 'u0027' : '', $flags & JSON_HEX_QUOT ? 'u0022' : '', ))); $pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/"; $callback = function ($m) { return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8'); }; return preg_replace_callback($pattern, $callback, json_encode($input, $flags)); }

    function raw_json_encode($ input,$ flags = 0){$ failed = implode('|',array_filter(array('\\\\',$ flags&JSON_HEX_TAG?'u003 [CE]':'',$ flags& JSON_HEX_AMP?'u0026':'',$ flags&JSON_HEX_APOS?'u0027':'',$ flags&JSON_HEX_QUOT?'u0022':'',))); $ pattern =“/ \\\\(?:(?:$ failed)(* SKIP)(* FAIL)| u([0-9a-fA-F] {4}))/”; $ callback = function($ m){return html_entity_decode(“&#x $ m [1];”,ENT_QUOTES,'UTF-8'); }; return preg_replace_callback($ pattern,$ callback,json_encode($ input,$ flags)); }

Example

<?php

function raw_json_encode($input, $flags = 0) {
    $fails = implode('|', array_filter(array(
        '\\\\',
        $flags & JSON_HEX_TAG ? 'u003[CE]' : '',
        $flags & JSON_HEX_AMP ? 'u0026' : '',
        $flags & JSON_HEX_APOS ? 'u0027' : '',
        $flags & JSON_HEX_QUOT ? 'u0022' : '',
    )));
    $pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
    $callback = function ($m) {
        return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
    };
    return preg_replace_callback($pattern, $callback, json_encode($input, $flags));
}

$json = array(
    'Sample' => array(
        'specialchars' => '<x>& \' "</x>',
        'backslashes' => '\\u0020',
        'context' => 'جمهوری اسلامی ایران',
    )
);

echo raw_json_encode($json, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);

/* 
{"Sample":{"specialchars":"\u003Cx\u003E\u0026 \u0027 \u0022\u003C\/x\u003E","backslashes":"\\u0020","context":"جمهوری اسلامی ایران"}}
*/

#1


1  

If you can't use JSON_UNESCAPED_UNICODE, you could probably unescape the JSON yourself after it's been encoded:

如果你不能使用JSON_UNESCAPED_UNICODE,你可能会在编码后自己忘记JSON:

  • Compatible with \ (escaped backslashes itself)
  • 兼容\(转义反斜杠本身)

  • Compatible with JSON_HEX_* flags

    与JSON_HEX_ *标志兼容

    function raw_json_encode($input, $flags = 0) { $fails = implode('|', array_filter(array( '\\\\', $flags & JSON_HEX_TAG ? 'u003[CE]' : '', $flags & JSON_HEX_AMP ? 'u0026' : '', $flags & JSON_HEX_APOS ? 'u0027' : '', $flags & JSON_HEX_QUOT ? 'u0022' : '', ))); $pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/"; $callback = function ($m) { return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8'); }; return preg_replace_callback($pattern, $callback, json_encode($input, $flags)); }

    function raw_json_encode($ input,$ flags = 0){$ failed = implode('|',array_filter(array('\\\\',$ flags&JSON_HEX_TAG?'u003 [CE]':'',$ flags& JSON_HEX_AMP?'u0026':'',$ flags&JSON_HEX_APOS?'u0027':'',$ flags&JSON_HEX_QUOT?'u0022':'',))); $ pattern =“/ \\\\(?:(?:$ failed)(* SKIP)(* FAIL)| u([0-9a-fA-F] {4}))/”; $ callback = function($ m){return html_entity_decode(“&#x $ m [1];”,ENT_QUOTES,'UTF-8'); }; return preg_replace_callback($ pattern,$ callback,json_encode($ input,$ flags)); }

Example

<?php

function raw_json_encode($input, $flags = 0) {
    $fails = implode('|', array_filter(array(
        '\\\\',
        $flags & JSON_HEX_TAG ? 'u003[CE]' : '',
        $flags & JSON_HEX_AMP ? 'u0026' : '',
        $flags & JSON_HEX_APOS ? 'u0027' : '',
        $flags & JSON_HEX_QUOT ? 'u0022' : '',
    )));
    $pattern = "/\\\\(?:(?:$fails)(*SKIP)(*FAIL)|u([0-9a-fA-F]{4}))/";
    $callback = function ($m) {
        return html_entity_decode("&#x$m[1];", ENT_QUOTES, 'UTF-8');
    };
    return preg_replace_callback($pattern, $callback, json_encode($input, $flags));
}

$json = array(
    'Sample' => array(
        'specialchars' => '<x>& \' "</x>',
        'backslashes' => '\\u0020',
        'context' => 'جمهوری اسلامی ایران',
    )
);

echo raw_json_encode($json, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);

/* 
{"Sample":{"specialchars":"\u003Cx\u003E\u0026 \u0027 \u0022\u003C\/x\u003E","backslashes":"\\u0020","context":"جمهوری اسلامی ایران"}}
*/