Solved

PHP unserialize returns empty array

Posted on 2007-03-30
9
1,876 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress French and English Site 6 78
How do uses indexes to maximize MySQL Searches 14 55
Need to remove %20 from url in get 17 36
php error 26 28
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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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