2

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_recursive($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?

clarkk
  • 27,151
  • 72
  • 200
  • 340

4 Answers4

3

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

Klaus S.
  • 1,239
  • 10
  • 18
  • In order to use array_walk_recursive with static methods you should instantiate directly: array_walk_recursive($arr, 'JSON::utf8_enc'); Or, if it's just a public method: array_walk_recursive($arr, array($this => 'utf8_enc')); – Klaus S. Apr 25 '11 at 14:50
  • I think you ment array_walk_recursive($arr, 'JSON::utf8_enc'); ? :) – clarkk Apr 25 '11 at 14:53
  • Your utf8_enc and parse_int methods are not declared static, by the way. – Klaus S. Apr 25 '11 at 14:55
  • Oh, and you can use __ CLASS __ to make it nicer. – Klaus S. Apr 25 '11 at 14:57
  • does all functions has to be declared static - even if they are only called from inside the class? – clarkk Apr 25 '11 at 14:57
  • If you're calling them as static methods, yes, they need to be static. Note that you're calling utf8_enc and parse_int as static methods. I don't see a reason for those methods to be static though. They could be just private methods. – Klaus S. Apr 25 '11 at 14:59
  • what do you mean ? __ CLASS __ – clarkk Apr 25 '11 at 14:59
  • CLASS is a magic constant: http://php.net/manual/en/language.constants.predefined.php – Klaus S. Apr 25 '11 at 15:14
2

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

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);
Klaus S.
  • 1,239
  • 10
  • 18
0

From PHP 5.4.0:

json_encode($objectToEncode, JSON_UNESCAPED_UNICODE);
4b0
  • 21,981
  • 30
  • 95
  • 142