is this json and how to replace values with php variables? - php

I have different php variables containing data that I want to put in one of the table in mysql database. The problem is I need to put the content in a specific format so another linked to the database can also use it.
I don't know what type of format is this. It doesn't look like JSON
a:1:{i:0;s:27:"my#example.com";}
a:17:{i:0;a:7:{s:2:"id";s:1:"1";s:4:"slug";s:19:"contact-information";s:4:"name";s:19:"Contact Information";s:4:"type";s:8:"fieldset";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:1;a:7:{s:2:"id";s:1:"6";s:4:"slug";s:12:"company-name";s:4:"name";s:12:"Company Name";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:20:"Company Name is here";}i:2;a:7:{s:2:"id";s:1:"8";s:4:"slug";s:12:"contact-name";s:4:"name";s:12:"Contact Name";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:8:"Name";}i:3;a:7:{s:2:"id";s:2:"10";s:4:"slug";s:13:"email-address";s:4:"name";s:13:"Email Address";s:4:"type";s:5:"email";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:26:"example#gmail.com";}i:4;a:7:{s:2:"id";s:2:"12";s:4:"slug";s:15:"website-address";s:4:"name";s:15:"Website Address";s:4:"type";s:3:"url";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:25:"http://example.com";}i:5;a:7:{s:2:"id";s:2:"15";s:4:"slug";s:13:"contact-phone";s:4:"name";s:13:"Contact Phone";s:4:"type";s:5:"phone";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:12:"923246573632";}i:6;a:7:{s:2:"id";s:2:"16";s:4:"slug";s:12:"your-address";s:4:"name";s:12:"Your Address";s:4:"type";s:7:"address";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:91:"House # 2, Street 27<br>Chah Mrain<br>Lahore, Punjab 54000<br>New Zealand";}i:7;a:7:{s:2:"id";s:2:"27";s:4:"slug";s:19:"listing-information";s:4:"name";s:19:"Listing Information";s:4:"type";s:8:"fieldset";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:8;a:7:{s:2:"id";s:2:"17";s:4:"slug";s:13:"listing-title";s:4:"name";s:13:"Listing Title";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:17:"House of the Dead";}i:9;a:7:{s:2:"id";s:2:"19";s:4:"slug";s:12:"listing-type";s:4:"name";s:12:"Listing Type";s:4:"type";s:6:"select";s:7:"options";s:90:"a:4:{i:0;s:11:"Attractions";i:1;s:13:"Accommodation";i:2;s:5:"Tours";i:3;s:9:"Transport";}";s:9:"parent_id";s:1:"0";s:5:"value";s:13:"Accommodation";}i:10;a:7:{s:2:"id";s:2:"20";s:4:"slug";s:17:"listings-overview";s:4:"name";s:19:"Listing\'s Overview";s:4:"type";s:8:"textarea";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:55:"Here is the overview of listing lets check what happens";}i:11;a:7:{s:2:"id";s:2:"21";s:4:"slug";s:10:"logo-image";s:4:"name";s:10:"Logo Image";s:4:"type";s:11:"file-upload";s:7:"options";s:31:"a:1:{i:0;s:13:"png|jpe?g|gif";}";s:9:"parent_id";s:1:"0";s:5:"value";s:80:"http://example.com/connect-icon.png";}i:12;a:7:{s:2:"id";s:2:"25";s:4:"slug";s:9:"longitude";s:4:"name";s:9:"Longitude";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:13;a:7:{s:2:"id";s:2:"26";s:4:"slug";s:8:"latitude";s:4:"name";s:8:"Latitude";s:4:"type";s:4:"text";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:14;a:7:{s:2:"id";s:1:"2";s:4:"slug";s:12:"verification";s:4:"name";s:12:"Verification";s:4:"type";s:12:"verification";s:7:"options";s:0:"";s:9:"parent_id";s:1:"0";s:5:"value";s:0:"";}i:15;a:7:{s:2:"id";s:1:"3";s:4:"slug";s:27:"please-enter-any-two-digits";s:4:"name";s:27:"Please enter any two digits";s:4:"type";s:6:"secret";s:7:"options";s:0:"";s:9:"parent_id";s:1:"2";s:5:"value";s:2:"14";}i:16;a:7:{s:2:"id";s:1:"4";s:4:"slug";s:6:"submit";s:4:"name";s:6:"Submit";s:4:"type";s:6:"submit";s:7:"options";s:0:"";s:9:"parent_id";s:1:"2";s:5:"value";s:0:"";}}
I just want to replace the value that I have in the variables and save it in database.

That format is the result of PHP's serialize() function. You can use unserialize() to decode it.

Related

PHP: Converting this data structure into an associative array

I have a field created by word press which contains data with the following structure.
a:16:{s:7:"country"
s:14:"United Kingdom"
s:7:"form_id"
s:2:"35"
s:9:"timestamp"
s:10:"1560869327"
s:7:"request"
s:0:""
s:8:"_wpnonce"
s:10:"125"
s:16:"_wp_http_referer"
s:1:"/"
s:17:"ajaxy-umajax-mode"
s:8:"register"
s:10:"first_name"
s:5:"xxxxx"
s:9:"last_name"
s:5:"xxx"
s:10:"user_email"
s:28:"xxx#xxx.co.uk"
s:7:"Company"
s:16:"xxx LTD"
s:12:"phone_number"
s:10:"0123456789"
s:8:"user_url"
s:20:"http://www.test.com"
s:15:"company_address"
s:18:"999 LockSmith Lane"
s:12:"display_name"
s:12:"XXXX"
s:10:"user_login"
s:10:"xxx123"
}
I want to convert this to an array so I can read the properties of it.
I tried converting it to json but its not json.
Any ideas on how I can parse this data, or access its properties in PHP.
I can not access this data through wordpress as my PHP script is part of something else.
Seems like a serialized array. Try unserializing it to convert it back to normal, see example.
This is Actually not a json.This is an array in serialized format you can just unserilize it by using this function
maybe_unserialize($YOUR_ARRAY).
maybe_unserialize is a wordpress default function to unserialize the array

How do I decode data in mysql field that is in this format?

I have fields in my database that I need to decode or view in a more simple format. I'm not sure what method was used to create this format though I've seen it before. This is data from a web form in a MySQL table. What do I use in PHP or MySQL to decode this when I retrieve it from the database?
a:10:{s:10:"First Name";s:10:"cvsgjsrhlw";s:9:"Last Name";s:10:"cvsgjsrhlw";s:7:"Address";s:26:"http://www.tlneepxlni.com/";s:4:"City";s:7:"Atlanta";s:5:"State";s:2:"AL";s:8:"Zip Code";s:0:"";s:9:"Best time";s:7:"Mid-day";s:6:"Other2";s:8:"cqoeqipd";s:14:"Procedure face";s:18:"Laser Hair Removal";s:4:"when";s:22:"In the next 4 months.";}
It's a built in php serialization
You need to use unserialize
This is serialize()'d code. It is a way to multiple data types as plain text. You can convert it back to a php object with unserialize($data)
If you are writing new code, I would recommend using json_encode() instead
This is a serialized array. Use:
$data_array = unserialize($data_from_db);

Parsing Form JSON output with PHP

How do I go about parsing the json request below in PHP to extract specific values (in value ) like the following: studentName, studentemail and courseName? There will be only one entry per request and I will not need the rest of the values. I need to store these as PHP variables to be written to MYSQL. The parsing is where I am having trouble
See json below
{"count":7,"data":[{"name":"Timestamp","ordinal":0,"value":"3/5/2013 2:45:31 PM"},{"name":"studentName","ordinal":1,"value":"mikejones"},{"name":"studentEmail","ordinal":2,"value":"mikejones%40gmail.com"},{"name":"courseName","ordinal":3,"value":"Grade%20Test"},{"name":"cpQuizInfoPassFail","ordinal":4,"value":"Fail"},{"name":"cpInfoPercentage","ordinal":5,"value":"75"},{"name":"institutionCode","ordinal":6,"value":"1212"}]}{"count":0}{"subject":"Course Transcript","footer":"Thank you for your participation!","footerURL":"https://sites.google.com/site/test393223837/googleformsdemotranscript/footerLogo.jpg","correctURL":"http//abc.com,"enabled":true,"correctResponseText":"Correct Response","showCorrectAnswers":true,"description":"Please keep this for your records as proof of course completion and certification.","incorrectURL":"https://sites.google.com/site/test3232393837/_/rsrc/1342229575450/googleformsdemotranscript/redX.png","fromName":"The Training Team","replyTo":"","headerURL":"https://sites.google.com/site/test3938232337/googleformsdemotranscript/CaptivateDev.jpg","isHTML":true,"title":"Course Transcript","to":"joe#gmail.com","userResponseText":"Your Response"}
You can use json_decode function, that parse json code to php objects and arrays. See documentation for more info.
One problem is your json is invalid. Check with:
http://jsonlint.com/ or
http://jsonformatter.curiousconcept.com/

How to Extract Data from a field with multiple values?

IP.Nexus puts custom fields into a table all muddled up into one row and some how pulls the correct data out.
basically my custom fields row looks like so:
a:2:{i:2;s:4:"Test";i:3;s:10:"Accounting";}
How do i select what item to pull as i would like to out put just one item not the whole row, is it with delimiters or string splitter, as i am novice with this part of mySQL
Cause i would like to echo out just the advertising on its own and also vice versa for the testing.
Thanks in advance
It's a serialized array using serialize() function so you can get its contents using unserialize($YourString);.
But I see that you string is corrupted for deserialization, because s:4: in s:4:"advertising" says that after s:4: 4 character long string is expected that advertising is not.
The data in the database is in serialized(json) form.You can turn it into simple array using unserialize.
For example;if $mystr is your string
write
$mystring=unserialize($mystring)
You will get normal array of this string
You can do
foreach($mystring as $str){
$ads[]=$str['advertising'];
}
and the resulting array $ads will be the ads you like in simple array form
Since this looks as JSon, you can use a PHP JSon module to parse each row from MySQL.
<?php
(...)
while (...) {
$obj = json_decode($row);
// do something with $obj
}

dump xml string verbatim to mysql db

I need to dump an xml string (coming into a web service as a POST param), directly into a Mysql DB. The column into which I am writing is currently of type 'text', though I have tried blob as well.
Right now, when I save the string and retrieve it later on using sql, it comes back in a serialized like format, like this:
a:1:{s:14:"<?xml_encoding";s:1502:"UTF-8?><record>
<nodes></nodes>
</record>";}
Whereas, I need to parse that xml as a simplexml object, and hence need it to be better formed.
This is what I am doing codewise:
Accept Post Param in web service
Serialize and store it in DB using doctrine ORM, kind of like
$record->xml_rec = serialize($_POST)
Retrieve it and echo it.
Oddly enough, if I unserialize and echo is upon retrial, I get an array. The array looks like this upon print_f
Array
(
[<?xml_encoding] => UTF-8?><record>
<nodes></nodes>
</record>
)
Still not parse-able by simplexml.
Is this a mysql storage problem or am I doing something wrong with the POST params? I've tried htmlspecialchars to save the string in the db, but similar problems remain.
any help is appreciated, thanks
What you have is a "serialized" php array that you can unserialize by using ... PHP's unserialize function.

Categories