?
Solved

PHP unserialize returns empty array

Posted on 2007-03-30
9
Medium Priority
?
2,004 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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