Solved

PHP unserialize returns empty array

Posted on 2007-03-30
9
1,817 Views
Last Modified: 2013-12-13
Hello,

I use serialize() to store an (maximum of three levels deep) array into a database field, which until now worked very fine. The data that is being stored, is mainly coming from a richt-text editor. The whole thing is used in a tool i have created for a client, which worked just very fine for almost a year now. However, just today my client has stored some information that for some reason can not be unserialized. PHP unserialize() just returns me nothing. An empty array, that is.

I now need to restore the information, so that it can be unserialized again, but still after hours of trying, i do not seem to find the error, nor do i seem to see any reason for the problem to occur.

My actual question is: how can i restore the information most efficiently? Is there any function that can analyze the broken data? Or an alternative to unserialize() that will not just return false but at least tell me where the problem resides, where it stops parsing?

I am looking forward to suggestions. Thank you very much.

Ps.: I do know about quoting problems and i can say for sure that it is none of those. In fact my client uses single and double quotes in his data frequently, but it has never been a problem. I made the script solid in that relation.
0
Comment
Question by:spiritwithin
  • 4
  • 4
9 Comments
 
LVL 29

Expert Comment

by:TeRReF
Comment Utility
Can you give an example of the serialized data?
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
Have you done a var_dump(...) on the serialized string ? It should at least give you an idea about what he stored.
0
 
LVL 1

Author Comment

by:spiritwithin
Comment Utility
TeRReF: Thanks for replying. The serialized data is just normal text/html data.

JakobA: Thanks for replying. var_dump() gives me as much as a print_r() - which is, unfortunately - nothing. Of course i took a look at the serialized data. It looks quite normal. The serialization closes correctly with the final bracket "}" and everything looks ok. Still it wont unserialize().
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
quotes or unbalanced bracketing are prime suspects, but without a specific example to look at all we can do is guess.
Quoting TeRReF: Can you give an example of the serialized data?
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:spiritwithin
Comment Utility
Alright. But i do not assume that this will really be of help, cause i cant just post of all of it here and you can not use only parts of it for testing (the full version is complete of course, fully enclosed by the brackets) :

a:17:{i:0;a:6:{s:5:'_type';s:5:'topic';s:3:'_id';i:0;s:4:'code';s:0:'';s:4:'text';a:1:{s:7:'content';s:0:'';}s:5:'topic';a:1:{s:7:'content';s:82:'<p><strong><u><font color='#ff6600' size='2'>RAUS-Verkauf!</font></u></strong></p>';}s:3:'img';s:0:'';}i:1;a:6:{s:5:'_type';s:3:'img';s:3:'_id';i:1;s:4:'code';s:0:'';s:4:'text';a:1:{s:7:'content';s:0:'';}s:5:'topic';a:1:{s:7:'content';s:0:'';}s:3:'img';s:21:'163__RAUS-Verkauf.gif';}i:2;a:6:{s:5:'_type';s:11:'last_minute';s:3:'_id';i:2;s:4:'code';s:0:'';s:4:'text';a:1:{s:7:'content';s:4592:'<p><span style='FONT-SIZE: 9pt; COLOR: #0067a6; FONT-FAMILY: Arial'>XXX XXXX bleibt seinem Motto treu und bietet dir erneut die <strong>besten Preise</strong>.<br />Da&nbsp;bleibt&nbsp;noch mehr Geld in deiner Bordkasse!<br /><br /></span><strong><span style='COLOR: red; FONT-FAMILY: Arial'><font size='3'><font color='#0067a6'>Buche jetzt und spare bis zu</font> </font><font size='6'>25%.</font></span></strong><b><span style='COLOR: red; FONT-FAMILY: Arial'><br /></span></b><span style='FONT-SIZE: 9pt; COLOR: #0067a6; FONT-FAMILY: Arial'><br />Unter dem Motto<strong> 'RAUS-Verkauf'</strong> bieten wir dir auch noch die <br /><strong>letzten freien Yachten</strong> um sensationelle Preise an. <br /><br />Hier ein kleiner Auszug der aktuellsten </span><strong><span style='COLOR: red; FONT-FAMILY: Arial'><font size='3'>Schnäppchen</font></span></strong><span style='FONT-SIZE: 9pt; COLOR: #0067a6; FONT-FAMILY: Arial'>:
<p></p></span></p>

-------------------

Just like that.

Ps.: The single quotes get replaced by double quotes before unserialize() is called, so this is not the matter.
0
 
LVL 15

Expert Comment

by:JakobA
Comment Utility
How about taking it a bit at a time. The serializing rules are nicely obvious so it should not be too hard.

Like here are the first 2 entries in serialized form:

a:2:{i:0;a:6:{s:5:'_type';s:5:'topic';s:3:'_id';i:0;s:4:'code';s:0:'';s:4:'text';a:1:{s:7:'content';s:0:'';}s:5:'topic';a:1:{s:7:'content';s:82:'<p><strong><u><font color='#ff6600' size='2'>RAUS-Verkauf!</font></u></strong></p>';}s:3:'img';s:0:'';}i:1;a:6:{s:5:'_type';s:3:'img';s:3:'_id';i:1;s:4:'code';s:0:'';s:4:'text';a:1:{s:7:'content';s:0:'';}s:5:'topic';a:1:{s:7:'content';s:0:'';}s:3:'img';s:21:'163__RAUS-Verkauf.gif';}}

if they unserialize correctly try the next 2, etc, until you find the data making trouble.

That is a pretty huge string by the way. do you have error_reporting enabled so you get iinformed in case it is just a basic error like 'out of memory, aborting operation' ?
0
 
LVL 15

Accepted Solution

by:
JakobA earned 500 total points
Comment Utility
this string is a bit suspect:

s:82:'<p><strong><u><font color='#ff6600' size='2'>RAUS-Verkauf!</font></u></strong></p>';

with '-s both in and around the string are you sure the single to double quote substitution gets done correctly there ?
0
 
LVL 1

Author Comment

by:spiritwithin
Comment Utility
Sorry for letting you wait, but i took a day off on the weekend.

Regarding the suspect string from above, i have checked the unserialize() - function and also checked other sets of data that just work fine. It appears that unserialize() does'nt mind the quotes inside of the quotes, since it the length of the string is provided. I have other sets of data, in which quotes and doublequotes appear, but they are no problem at all.

I will try to put together a list of parts according to your suggestion above and try to find the problem that way.
0
 
LVL 1

Author Comment

by:spiritwithin
Comment Utility
Well, i was not able to figure out the error, and for the client it was more efficient to just redo the whole thing (it was a newsletter) based on the pieces of data i could provide him with. Therefore i can not seriously consider any answer to be the correct solution, so i will just choose any and provide you with the points. Thanks for trying to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now