Popweb Posted May 2, 2014 Share Posted May 2, 2014 Hi there.Does anyone here have an idea to how I can extract this to an array: {s:4:"navn";s:18:"Some data";s:5:"alder";s:33:"Some data";s:5:"email";s:17:"Some data";s:5:"kirke";s:45:"Some data";s:17:"praktisk-baggrund";a:1:{i:0;s:71:"Some data";}s:17:"instrument-toggle";a:1:{i:0;s:2:"Ja";}s:10:"instrument";s:22:"Some data";s:10:"min-stemme";s:35:"Some data";s:9:"kommentar";s:106:"Some data";s:19:"vaerelses-fordeling";s:0:"";s:18:"vegetar-eller-lign";s:0:"";s:15:"FUK-laereplaner";a:1:{i:0;s:0:"";}} It is a string picked up in a Wordpress database. Wordpress plugin. I dont need all the s:# I just need "navn": "Some data", "alder": "Some data", "email": "Some data". ("Some data" obviously varying). $theArray['navn']="Some data"; $theArray['alder']="Some data"; and so forth Can you help? Thanks John Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 2, 2014 Share Posted May 2, 2014 http://us1.php.net/json_decode Quote Link to comment Share on other sites More sharing options...
Popweb Posted May 2, 2014 Author Share Posted May 2, 2014 So are you saying that the string IS json-encoded? I didnt recognize it as being json.... And if I run json(the string); And then try to print_r the result I get nothing.... ?? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 2, 2014 Share Posted May 2, 2014 This is not JSON. It looks like the serialization format of PHP, but then it's incomplete. The “a:<number of elements>:” in front of the outermost array is missing, so you can't just run this through unserialize(). Are you sure this is the exact string from the database? How does the plugin parse the string? Quote Link to comment Share on other sites More sharing options...
Popweb Posted May 2, 2014 Author Share Posted May 2, 2014 Ah! Your're right! I tried removing the first part - to try to make it look more json-like. This is the full string: a:12:{s:4:"navn";s:18:"Some data";s:5:"alder";s:33:"Some data";s:5:"email";s:17:"Some data";s:5:"kirke";s:45:"Some data";s:17:"praktisk-baggrund";a:1:{i:0;s:71:"Some data";}s:17:"instrument-toggle";a:1:{i:0;s:2:"Ja";}s:10:"instrument";s:22:"Some data";s:10:"min-stemme";s:35:"Some data";s:9:"kommentar";s:106:"Some data";s:19:"vaerelses-fordeling";s:0:"";s:18:"vegetar-eller-lign";s:0:"";s:15:"FUK-laereplaner";a:1:{i:0;s:0:"";}} Aha... so it may be serialized? I tried to unserialize(the string);And got: Error at offset 49 of 600 Any ideas? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 2, 2014 Share Posted May 2, 2014 (edited) I tried removing the first part - to try to make it look more json-like. Not exactly the smartest idea, especially when you ask people to identify the data. This is the full string: Not really. Obviously you've again changed the actual data and replaced it with your “Some data” dummy string. We cannot help you when you only give us some fantasy data which is totally different from your actual data. How are we supposed to debug this? I understand that you may not want to hand out your real data, but then you'll have to fix it yourself. Edited May 2, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted May 2, 2014 Share Posted May 2, 2014 The string looks okay as serialized data. More likely than not you have a value that's not the same length as the s: part. I would double check your test data to make sure these match. Ah! Your're right! I tried removing the first part - to try to make it look more json-like. This is the full string: a:12:{s:4:"navn";s:18:"Some data";s:5:"alder";s:33:"Some data";s:5:"email";s:17:"Some data";s:5:"kirke";s:45:"Some data";s:17:"praktisk-baggrund";a:1:{i:0;s:71:"Some data";}s:17:"instrument-toggle";a:1:{i:0;s:2:"Ja";}s:10:"instrument";s:22:"Some data";s:10:"min-stemme";s:35:"Some data";s:9:"kommentar";s:106:"Some data";s:19:"vaerelses-fordeling";s:0:"";s:18:"vegetar-eller-lign";s:0:"";s:15:"FUK-laereplaner";a:1:{i:0;s:0:"";}} Aha... so it may be serialized? I tried to unserialize(the string);And got: Error at offset 49 of 600 Any ideas? Quote Link to comment Share on other sites More sharing options...
Popweb Posted May 2, 2014 Author Share Posted May 2, 2014 And Jaques... well...that was sort of rude?! ... Obviously I didn't intend to not show the entire string?! I removed the first part to test. And forgot to paste it back in, when asking for help. Dont tell me you never make mistakes? Yes I replaced the data with "Some data". But not to hide anything. That was to make the string more readable. Another mistake. I am sorry about that. Here is one of the actual strings: (Except: I changed the users email adress. but I counted the new number of chars and changed the value.) a:12:{s:4:"navn";s:18:"Margit Anna Brodam";s:5:"alder";s:33:"13 år når sommerskolen afholdes";s:5:"email";s:19:"adresse@adresse.com";s:5:"kirke";s:45:"Asminderød Kirke / personligt meldlem af FUK";s:17:"praktisk-baggrund";a:1:{i:0;s:71:"Jeg kender alle nodeværdier og tonenavne og kan klappe noterede rytmer";}s:17:"instrument-toggle";a:1:{i:0;s:2:"Ja";}s:10:"instrument";s:22:"Violin begynder niveau";s:10:"min-stemme";s:35:"Dyb 2. sopran med smuk stor klang ";s:9:"kommentar";s:106:"Cille Buch er Margits korlærer. Margit har sunget i kirkekor i 5½ år og tillige i koncertkoret i 2 år.";s:19:"vaerelses-fordeling";s:0:"";s:18:"vegetar-eller-lign";s:0:"";s:15:"FUK-laereplaner";a:1:{i:0;s:0:"";}} I counted the size of the different vars and checked the s-numbers. Turns out that every time a danish letter (æ, ø and å) appears in the string, the number differs from the actual number of chars. I suppose that might be it? The number is too big, when there are danish chars in the vars. Any suggestions? Thank you all for taking the time to read, and try to help out.Even if you are being rude. ;-)I need the help, and I dont have to pay for it - so ofcourse I have to deal with whatever response I get. :-) Thx John Quote Link to comment Share on other sites More sharing options...
Solution Popweb Posted May 2, 2014 Author Solution Share Posted May 2, 2014 AHA! A little experimenting later: Turns out that I can unserialize, if I mb_convert_encoding() the string to utf-8 first. Mystery solved. Thx John Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.