Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Format:forge() decoding dose not work
  • Hello there,

    I have following data to encode and decode.

    $data = array(
    0 => 'あああ',
    1 => 'いいい'
    );

    Then I can encode that array by followings.
     \Format::forge($data)->to_json();
     \Format::forge($data)->to_serialized();

    However, I cannot decode by use of following code, namely empty array.

     \Format::forge($data,'json')->to_array();
     \Format::forge($data,'serialize')->to_array();

    Do you know have any suggestion for this?

    Here is my current environment.

    PHP 7.1
    Ubuntu 16.04 LTS
    FuelPHP 1.9-dev

  • This is json encode data.
    ["\u3042\u3042","\u3044\u3044"]

    The json_last_error_msg() says "Syntax error".


    This is serialized data.
    a:2:{i:0;s:6:"ああ";i:1;s:6:"いい";}

  • For the json data, I figure out.

    \Format::forge(str_replace('"', '"',$data),'json')->to_array();

    OR

    \Format::forge(html_entity_decode($data),'json')->to_array();

    I am just wondering why this is happening....

  • Works fine here.

    You see escaped unicode because that is the default behaviour of json_encode(). 

    PHP 5.4+ has an option to disable that behavior, but as that makes the result less portable, the Format class doesn't use it.

    Test code:
    <?php
    $data = array(0 => 'あああ', 1 => 'いいい');

    $a = json_encode( $data );
    echo json_last_error_msg();
    var_dump($data, $a);

    $a = \Format::forge($data)->to_json();
    $b = \Format::forge($data)->to_serialized();

    var_dump($data, $a, $b);

    $a = \Format::forge($a, 'json')->to_array();
    $b = \Format::forge($b, 'serialize')->to_array();

    var_dump($data, $a, $b);


    Output:

    [wanwizard@catwoman] $ env FUEL_ENV=local oil console
    Fuel 1.9-dev - PHP 7.3.9 (cli) (Aug 27 2019 22:52:39) [Linux]
    >>> require "/tmp/test.php";
    No error /tmp/test.php:6:
    array(2) {
      [0] =>
      string(9) "あああ"
      [1] =>
      string(9) "いいい"
    }
    /tmp/test.php:6:
    string(43) "["\u3042\u3042\u3042","\u3044\u3044\u3044"]"
    /tmp/test.php:11:
    array(2) {
      [0] =>
      string(9) "あああ"
      [1] =>
      string(9) "いいい"
    }
    /tmp/test.php:11:
    string(43) "["\u3042\u3042\u3042","\u3044\u3044\u3044"]"
    /tmp/test.php:11:
    string(46) "a:2:{i:0;s:9:"あああ";i:1;s:9:"いいい";}"
    /tmp/test.php:16:
    array(2) {
      [0] =>
      string(9) "あああ"
      [1] =>
      string(9) "いいい"
    }
    /tmp/test.php:16:
    array(2) {
      [0] =>
      string(9) "あああ"
      [1] =>
      string(9) "いいい"
    }
    /tmp/test.php:16:
    array(2) {
      [0] =>
      string(9) "あああ"
      [1] =>
      string(9) "いいい"
    }

  • Maybe you haven't loaded PHP's mbstring extension?
  • Thanks for your quick reply.

    mbstring extension is already loading when I check phpinfo().

    Then I found one thing.

    The source you provided working fine here as well.

    But, when I insert that string data into DB by orm and fetch from it, this thing happen.

    The file and DB encode is UTF-8.

    This is so strange for me....

    Do you have any suggestion for this?
  • What DB are you using? What is the type of column you store the data in? What is the exact DB character encoding used for the database, table and column? 

    What if you access that column using a tool like PHPMySQLAdmin or Adminer?
  • Sorry for late reply.
    The column type is medium text.
    We use MySQL and character encoding is utf-8_general_ci.
    When we get the data from the tool, it seems okay.
    The tool we use is A5:SQL.
    This is strange for me.
  • A5:SQL is a Windows tool, and Windows doesn't understand utf-8 unless the application explicitly does conversions.

    Maybe that is your problem?
  • The data is stored as json and it seems okay for me.

    I checked by console as well.

    But, when I fetched by orm, escaped unicode thing happened.

    Well....is that related to following config?

    'output_filter'  => array('Security::htmlentities')


  • The output filter is used on data send to a view. 

    So if you are using views instead of Controller_Rest to output json, yes, that can have an influence.

    If you want to pass the data to the view in raw form, you have to set the appropriate flag.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion