Solved

PHP unserialize returns empty array

Posted on 2007-03-30
9
1,855 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
ID: 18825962
Can you give an example of the serialized data?
0
 
LVL 15

Expert Comment

by:JakobA
ID: 18826134
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
ID: 18826292
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 15

Expert Comment

by:JakobA
ID: 18826452
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
 
LVL 1

Author Comment

by:spiritwithin
ID: 18826570
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
ID: 18826730
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
ID: 18826741
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
ID: 18835303
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
ID: 18856260
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
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.

785 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