开发者

æøå in json_encode()

开发者 https://www.devze.com 2023-03-01 01:54 出处:网络
how can it be that strings containing æøå or other special chars is returned as null?? not only the special char is leaved but the whole string (value) is returned as null...

how can it be that strings containing æøå or other special chars is returned as null?? not only the special char is leaved but the whole string (value) is returned as null...

EDIT:

class JSON {
    static function encode($arr, $utf8_encode=false){
        $arr = self::parse_int($arr);

        if($utf8_encode){
            array_walk_recu开发者_开发知识库rsive($arr, array(self => 'utf8_enc'));
        }

        return $arr ? json_encode($arr):'{}';
    }

    static function decode($str){
        return json_decode($str, true);
    }

    function utf8_enc(&$value, $key){
        $value = utf8_encode($value);
    }

    function parse_int($arr){
        foreach($arr as $key => $value){
            if(is_array($value)){
                $arr[$key] = self::parse_int($value);
            }
            else{
                if(is_numeric($value)){
                    settype($value, 'float');
                }

                $arr[$key] = $value;
            }
        }

        return $arr;
    }
}

but I get this error:

Warning: array_walk_recursive() expects parameter 2 to be a valid callback, array must have exactly two members

in this line:

array_walk_recursive($arr, array(self => 'utf8_enc'));

how do you define a function in the current object?


All strings sent to the json_encode function should be UTF8 encoded. No exceptions. You can use iconv or utf8_encode to help you.


json_encode requires that strings fed into it are encoded in UTF-8. If you are calling it and passing such characters in any single-byte encoding, the return value is null.


Well, i took your class and improved it a little bit. There's no reason for having internal class methods declared as static. Your encode and decode methods can be static, but parse_int and utf8_enc can be simple private methods.

<?php

class JSON {
    public function encode($arr, $utf8_encode = false) {
        $arr = $this->parse_int($arr);

        if ($utf8_encode) {
            array_walk_recursive($arr, array($this, 'utf8_enc'));
        }

        return $arr ? json_encode($arr) : '{}';
    }

    public function decode($str) {
        return json_decode($str, true);
    }

    private function utf8_enc(&$value, $key) {
        $value = utf8_encode($value);
    }

    private function parse_int($arr)
    {
        foreach ($arr as $key => $value) {
            if (is_array($value)) {
                $arr[$key] = $this->parse_int($value);
            } else {
                if (is_numeric($value)) {
                    settype($value, 'float');
                }
                $arr[$key] = $value;
            }
        }

        return $arr;
    }
}

$json = new JSON;
echo $json->encode(array('áé$@(*&dásásd?eq'), true);


From PHP 5.4.0:

json_encode($objectToEncode, JSON_UNESCAPED_UNICODE);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号