Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
Format:forge() decoding dose not work
soseki
September 2019
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
soseki
September 2019
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:"いい";}
soseki
September 2019
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....
Harro
September 2019
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) "いいい"
}
Harro
September 2019
Maybe you haven't loaded PHP's mbstring extension?
soseki
September 2019
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?
Harro
September 2019
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?
soseki
October 2019
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.
Harro
October 2019
A5:SQL is a Windows tool, and Windows doesn't understand utf-8 unless the application explicitly does conversions.
Maybe that is your problem?
soseki
October 2019
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')
Harro
October 2019
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.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
October 2019
soseki
October 2019