serialization and utf-8 in PHP - php

Hi All
I'm trying to serialize array that contains some utf-8 code:
....["value"]=> string(13) "مغادرة1"....
but after serializing the array,it look like this:
value";s:13:"??????
I think that the error that i get:
Message: unserialize() [function.unserialize]: Error at offset 685 of 701 bytes
is related to wrong serialization to utf-8 code
So How to serialize array that contains utf-8 code?
Thank You

Thank's for all replies
The problem was in storing the data in the database,and not in serialization,the type of field that i stored the ser content was latin,i change it to utf-8 and everything works fine

Related

How is json decoding of utf emoji possible?

As far as i call recall. A valid Json data comes in this format '{"key":"value"}'
But while surfing, I found an article about sending UTF-8 codes as emoji.
Emoji was stored in variable as
$emoji = "\ud83d\udc4e";. For it to work properly, the answer was to use json_decode($emoji);. I tried it out and it returned a thumbs down emoji. Meanwhile, I was expecting NULL but it turns out that it was a valid json data. So I'm confused how that is possible.

Converting Hebrew characters to UTF-8 using PHP

i have a MySQL field value with a json object containing Hebrew characters like this:
[{"name":"אספנות ואומנות","value":1,"target":null},{"name":"אופניים","value":2,"target":null}]
(the one in the name field)
This field output is giving me some trouble with a certain web interface.
so, looking around in the database i found another field containing json object and its output works fine.
[{"name":"\u05d0\u05e1\u05e4\u05e0\u05d5\u05ea \u05d5\u05d0\u05d5\u05de\u05e0\u05d5\u05ea","value":1,"target":null},{"name":"\u05d0\u05d5\u05e4\u05e0\u05d9\u05d9\u05dd","value":2,"target":null}]
So i would like to convert the first field to this encoding to see if its solves the output issue.
what is this encoding ? is it UTF-8 ? how can i convert it using PHP ?
i tried to isolate the value and convert it to UTF-8 using
echo iconv("Windows-1255","UTF-8",'אספנות ואומנות');
but its just returning an empty value.
Any help would be great
So, in PHP
json_encode('אספנות ואומנות');
did the trick

Converting Array Values from ISO-8859-1 to UTF8 for JSON Encoding

I receive a response from a web service, and the values are ISO-8859-1 encoded. The supplied library converts the response to an array, and I am using json_encode to serialize the response and store it in the database. It's a MySQL database, InnoDB engine, with a UTF8 character set.
The encoding issue revealed itself by this warning in my log:
PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in...
I know the array key values are alright because they're always the same and I don't see the warning for every request.
Is there any downside to using array_map with utf8_encode to get rid of the warnings?
json_encode(array_map('utf8_encode', $response))
You will anyway need to call it systematically. So go for it.

PHP unserialize() Error at Offset [duplicate]

This question already has answers here:
How to repair a serialized string which has been corrupted by an incorrect byte count length?
(17 answers)
Closed 9 years ago.
The exact error I am getting is:
ErrorException [ Notice ]: unserialize(): Error at offset 5 of 59 bytes
The serialized data returned from a TEXT field in MySQL is (Encoding: utf8, Engine: InnoDB):
a:1:{s:12:"display_name";s:6:"Foo";}
After unserializing the output is:
array(1) { [0]=> bool(false) }
I am expecting something like this:
array(1) { ["display_name"]=> string(6) "Foo" }
I am using FuelPHP 1.6.1, PHP 5.4.10, MySQL 5.5.29, InnoDB 1.1.8 on Mac OS 10.8.4. Some of the serialized entries work on unserialize while others don't, if I copy one that works and paste into one that does not it shows the same error. I believe its a character issue and I have tried to encode into utf8, urlencode, and stripslashes but nothing seems to work.
Any help is appreciated!
Update
The serialized string had a type, I did this when pasting it here for privacy to the actual display name. Here is the actual string:
a:1:{s:12:"display_name";s:3:"Foo";}
Thanks to #robw for pointing this out.
Update & Answer
I am running the serialized data through html_entity_decode() now as such:
$unserialized = unserialize(html_entity_decode($serialized, ENT_QUOTES));
Thank you for the help and advice!
Your problem is here: s:6:"Foo"
Foo is not 6 characters long... so that will never parse as expected.
Try: s:3:"Foo"
PS: This looks like you edited data in MySQL, then when your application tried to parse it, it errored out. You should never edit a serialized value in the database unless you're 100% sure you've modified the LENGTH in accordance to the TYPE and VALUE.

unserializing data array with php from mysql database

I have the following serialized PHP array stored in a mySQL database:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}
Now, I managed to output that value with:
$my_data=mysql_result($result,$i,"my_data");
echo "$my_data";
but I can't manage to unserialize it. I tried this but it doesn't work:
$my_data=unserialize($my_data);
When I add that in between, all I get is a blank page. Any ideas?
Maybe you should look at the process of inserting the the value into the database. Is it possible that after the values are serialized, that they were encoded in someway, such as to html entities or something?
I ran a test locally and I got the same error message. Here is output:
a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";} Notice: unserialize(): Error at offset 12 of 62 bytes in /srv/localhost/public_html/test.php on line 6
Here is code
<?php
$value = htmlentities('a:2:{i:2070;s:4:"0.00";i:1901;s:4:"1.00";}');
echo $value;
unserialize($value);

Categories