I import data from WordPress. It is a WebDirectory 2.0 that saves the data as a "strange" string in a database.
Example:
s:95:"a:2:{s:3:"url";s:39:"http://www.google.com/";s:4:"text";s:13:"Website";}";
I have tried json_decode() and unserialize(). Both do not work. They return the string as it is. I can see, this is an array with two values, but how this can be parsed in PHP? In what format this string can be?
If you have this:
echo serialize(serialize(['url'=>'http://www.google.com/','text'=>'Website']));
The output will be
s:71:"a:2:{s:3:"url";s:22:"http://www.google.com/";s:4:"text";s:7:"Website";}";
But what you have is
s:95:"a:2:{s:3:"url";s:39:"http://www.google.com/";s:4:"text";s:13:"Website";}";
Which is different. See, s:95 != s:71, among other elements.
Or this is not a real data or by some reason it's generating extra chars, and causing the problem. I would check how the encoding from both PHP and database to see if all matches.
Related
I am using a Yii2 model that is hooked to a PostgresQL database. I have a behavior that encodes and decodes certain attributes of this model to/from json. To encode/decode, I am using the Json helper, the Json::encode and Json::decode methods.
The column in the table is of a json type. An example of what ends up in the database:
"{\"additional_tags\":[\"#здрасте\",\"#кафе\"],\"vk\":\"vk.com\\\/privetik\"}"
When I try to decode it back into a php array, here's what's returned instead:
'{"additional_tags":["#здрасте","#кафе"],"vk":"vk.com\/privetik"}'
EDIT: Come to think of it, the string seems fine, but the behavior of the ::decode method is strange. Essentially, all it does is remove the escape slashes, instead of converting it into a php array or throwing an exception.
What should I do to fix this? Appreciate any feedback.
Looks like the string in your database is encoded twice. Try passing it through Json::decode another time, I bet it will return your array.
I am trying to pass an array through a url. I have tried encoding the URL, serializing the URL, serializing and encoding the URL, and no matter what I do, the length of the strings is coming up in the url.
For example, if I pass the array through a URL this way:
<a href='http://splitsum.com/samples/your_store/checkout_form2.php?arr=<?PHP echo serialize($order); ?>'>Next Page</a>
The resulting URL looks like this (with the string count printed out):
.....s:15:%22shipping_method%22;s:20:%22Flat%20Rate%20(Best%20Way)%22;.....
Does anyone know why this is happening? I can var_dump the entire array (and see the string counts on the page) but I cant seem to print individual values in the array. Could it have something to do with a problem in the URL and the printing of the string length?
Thanks!
Because you're using serialize(). You should be using urlencode() instead.
serialize is intended to take internal arbitrary data structures, and encode them into a portable format for re-use in a PHP system somewhere else. It does NOT produce code that is guaranteed valid in a URL context. Basically you're using a hammer to pound in a screw. Use a screwdriver instead.
Note that urlencode will not accept an array. Perhaps http_build_query() would be more appropriate
The length is appearing because you're using serialize. That how it outputs. It's used to store a PHP variable, so that it can be loaded back into PHP again. It's output format contains the length of arrays/strings.
This is the wrong tool for this job. You want to use http_build_query here instead.
<a href='http://splitsum.com/samples/your_store/checkout_form2.php?<?PHP echo http_build_query(array('arr' => $order)); ?>'>Next Page</a>
Then $_GET['arr'] in checkout_form2.php will be your $order array.
Is there a PHP function to encode a generic multidimensional array as a string and get it back as a multidimensional array?
I need it to store in mysql some data (a drupal computed field to be precise). The array contains just floats and strings.
serialize() and unserialize() do what you describe.
http://www.php.net/manual/en/function.serialize.php
http://www.php.net/manual/en/function.unserialize.php
You could also consider encoding the array as JSON with json_encode() and json_decode(), which gives more readable output, if that is important to you.
I second the use of "json_encode" and "json_decode". I believe the output of "json_encode" is less verbose than PHP's serialize function (since data types are inferred) and is immediately more portable (even though that is not a requirement).
Make sure you pass "TRUE" for the second parameter of "json_decode", otherwise you may get a simple object back, depending on how the original data was encoded.
Hey can anyone tell me what datatype this is? it wont parse as i've stripped out sensitive data. Am I correct in thinking its json?
a:7:{s:12:"competitions";a:893:{i:1;s:10:"Ersie";i:5;s:19:"General
News"1510126584;s:0:"";i:1019;s:0:"";i:8284;s:0:"";i:191016;s:0:"";i:284;s:0:"";i:91019;s:0:"";i:81863;s:0:"";i:1563;s:0:"";i:710138;s:0:"";i:101333;s:0:"";i:33430;s:0:"";i:10224;s:0:"";i:10430;s:0:"";i:13430;s:0:"";i:375;s:0:"";i:72107;s:0:"";i:11372;s:0:"";i:181372;s:0:"";i:1885;s:0:"";i:107155;s:0:"";i:10284;s:0:"";i:8206;s:0:"";i:8101316;s:0:"";i:1913;s:0:"";i:206;s:0:"";i:772138;s:0:"";i:72284;s:0:"";i:1672155;s:0:"";i:7101663;s:0:"";i:891013;s:0:"";i:101843;s:0:"";i:1107;s:0:"";i:1072;s:0:"";i:830;s:0:"";i:7284;s:0:"";i:8101333;s:0:"";i:13372;s:0:"";i:1570;s:0:"";i:2543;s:0:"";i:91316;s:0:"";i:385;s:0:"";i:8385;s:0:"";i:2843;s:0:"";i:695;s:0:"";i:1970;s:0:"";i:1661;s:0:"";i:18206;s:0:"";i:33155;s:0:"";i:787;s:0:"";i:8117;s:0:"";i:1943;s:0:"";i:3043;s:0:"";i:872239;s:0:"";i:872155;s:0:"";i:910104;s:0:"";i:8125;s:0:"";i:239;s:0:"";i:8428;s:0:"";i:1382;s:0:"";i:87293;s:0:"";i:95385;s:0:"";i:11672;s:0:"";i:92572;s:0:"";i:828;s:0:"";i:8239;s:0:"";i:640;s:0:"";i:87155241;s:0:"";i:26155241;s:0:"";i:87158241;s:0:"";i:69158241;s:0:"";i:82543;s:0:"";i:193372;s:0:"";i:90163241;s:0:"";i:163372;s:0:"";i:1016107;s:0:"";i:86372;s:0:"";i:87163241;s:0:"";i:155162241;s:0:"";i:713121;s:0:"";i:2690241;s:0:"";i:895430;s:0:"";i:-403549467;s:0:"";i:2060490689;s:0:"";i:7181924;s:0:"";i:157158241;s:0:"";i:87295;s:0:"";i:71672430;s:0:"";i:1372430;s:0:"";i:9430;s:0:"";i:897;s:0:"";i:242;s:0:"";i:87162241;s:0:"";i:161863;s:0:"";i:1824107;s:0:"";i:26165241;s:0:"";i:7891316;s:0:"";i:81670;s:0:"";i:43107;s:0:"";i:710121;s:0:"";i:172283;s:0:"";i:8790241;s:0:"";i:253372;s:0:"";i:71863;s:0:"";i:26157241;s:0:"";i:8393;s:0:"";i:91824;s:0:"";i:826;s:0:"";i:63640;s:0:"";i:26163241;s:0:"";i:13121;s:0:"";i:82574;s:0:"";i:165241;s:0:"";i:87157241;s:0:"";i:2687241;s:0:"";i:26162241;s:0:"";i:199;s:0:"";i:18910;s:0:"";i:162165241;s:0:"";i:751430;s:0:"";i:16125;s:0:"";i:81893;s:0:"";i:79101316;s:0:"";i:81693;s:0:"";i:8913119;s:0:"";i:7818;s:0:"";i:72223;s:0:"";i:781072;s:0:"";i:972107;s:0:"";i:26241;s:0:"";i:90158241;s:0:"";i:87071;s:0:"";i:816125;s:0:"";i:72155430;s:0:"";i:72109;s:0:"";i:8106;s:0:"";i:181;s:0:"";i:8126;s:0:"";i:897293;s:0:"";i:187;s:0:"";i:101318;s:0:"";i:7166372;s:0:"";i:13216;s:0:"";i:101367;s:0:"";i:91149;s:0:"";i:781633;s:0:"";i:7101363;s:0:"";i:872430;s:0:"";i:1657;s:0:"";i:215;s:0:"";i:1025;s:0:"";i:7136372;s:0:"";i:1314;s:0:"";i:101319;s:0:"";i:1372344;s:0:"";i:1372573;s:0:"";i:1672430;s:0:"";i:872109;s:0:"";i:972430;s:0:"";i:193372430;s:0:"";i:13171;s:0:"";i:172430;s:0:"";i:6372430;s:0:"";i:772430;s:0:"";i:1372155430;s:0:"";i:71372;s:0:"";i:887;s:0:"";i:78933344;s:0:"";i:8131672;s:0:"";i:131672;s:0:"";i:365;s:0:"";i:91172;s:0:"";i:772155;s:0:"";i:972155;s:0:"";i:137072;s:0:"";i:729;s:0:"";i:781672;s:0:"";i:891172;s:0:"";i:924;s:0:"";i:796372;s:0:"";i:94372;s:0:"";i:284385;s:0:"";i:72344;s:0:"";i:71372121;s:0:"";i:78972;s:0:"";i:136372;s:0:"";i:713211;s:0:"";i:17972;s:0:"";i:101872;s:0:"";i:17872;s:0:"";i:84370;s:0:"";i:71633;s:0:"";i:172107;s:0:"";i:833732;s:0:"";i:18101318;s:0:"";i:816155;s:0:"";i:83343;s:0:"";i:43155;s:0:"";i:438;s:0:"";i:104363;s:0:"";i:961;s:0:"";i:843732;s:0:"";i:78107;s:0:"";i:13344;s:0:"";i:72393;s:0:"";i:83363;s:0:"";i:113121;s:0:"";i:863121;s:0:"";i:8131643;s:0:"";i:518;s:0:"";i:1789;s:0:"";i:61107;s:0:"";i:891619;s:0:"";i:43214;s:0:"";i:63121;s:0:"";i:7136430;s:0:"";i:81865;s:0:"";i:728430;s:0:"";s:11:"10245074125";s:0:"";i:63162;s:0:"";i:393;s:0:"";i:89732;s:0:"";i:2633430;s:0:"";i:157283430;s:0:"";}s:8:"lastpost";i:1281041491;s:7:"default";s:19:"General
News";s:11:"autopublish";s:2:"on";s:5:"draft";N;s:6:"poster";s:1:"1";s:6:"suffix";s:0:"";}
This is a serialized PHP array. Essentially, it's just a string that represents a PHP array (the a at the beginning marks it as an array).
You can get the PHP array back out of it by passing the string to the PHP function unserialize().
That is serialized data - not Wordpress specific.
Take a look into serialize(), unserialize() and OOP serialization: __sleep()/__wakeUp().
Some have see that code :
<?php
echo stripslashes(json_encode(glob("photos-".$_GET["folder"].'/*.jpg')));
?>
it echo a really nice perfect string like that :
["photos-animaux/ani-01.jpg","photos-animaux/ani-02.jpg","photos-animaux/ani-02b.jpg","photos-animaux/ani-03.jpg","photos-animaux/ani-04.jpg","photos-animaux/ani-05.jpg","photos-animaux/ani-06.jpg","photos-animaux/ani-07.jpg","photos-animaux/ani-08.jpg","photos-animaux/ani-09.jpg","photos-animaux/ani-10.jpg","photos-animaux/ani-11.jpg","photos-animaux/ani-12.jpg","photos-animaux/ani-13.jpg","photos-animaux/ani-14.jpg"]
With json encode it shoul send a array so variable[0] should = to photos-animaux/ani-01.jpg
NOW it is only the fisrt caracter of the string..
how a array get converted to string, and how in javascipt to convert string to array to be able to get value [0] [1] etc..
the question is WHEN or WHY the perfect array get converted to string anyway ?
Using JSON.parse(), from the library available here, is preferable to using eval() since this only reads JSON strings and hence avoids the inherant security risks associated with eval().
In order to parse a JSON-encoded string into actual javascript objects, you need to eval() the data returned in order for javascript to execute against the data:
var data = eval(json_string)
Keep in mind that you should only ever run eval on a string when you trust the source and are sure that there is no possibility of a script injection attack, since javascript will run everything present in the string.
Use one of the JSON libraries listed at the bottom of http://json.org/
WHEN or WHY
If I understood correctly, the answer to both is json_encode(): check out the documentation. Converting a variable to a string (often called serialization or flattening) is a quite common operation.