Sublime text beautify PHP serialized data - php

I deal with serialized PHP data a lot (wp developer) and I'd like to know if anyone out there has figured out how to beautify serialized php data from within ST3. I'd like to be able to view my serialized php data in a nice indented format in the same way that I can already view my JSON and other serialized data formats. There are plenty of tools online that do this, but i'd like on integrated into ST3.
There is a PHP beautifier out there which i believe runs the PHP_Beautifier PEAR package, but it does nothing to serialized data (probably because the extra whitespace would break the format).
I have had relative luck with copying the data to a new file and running a regex replace /(.*?);(.*?);/$1 : $2,/ and then running Format as Javascript from CodeFormatter. but obviously this quite a bit of work to do each time.
I'm new to ST3 so I'd like some opinions on what my best option is here. Is there a solution out there or should I create a plugin to send the string to an online beautifier, or write a macro or something?
Cheers

I am afraid that PHP serialized data cannot be formatted (like JSON or XML)
The PHP serialization format does not allow additional white-space between the serialization token. The only thing you can do is to unserialize the data for display and then re-serialize it before saving the file.

Related

Is there a XML decoder for PHP objects?

I am build a application in Delphi that stores some configuration that is not going to be passive to query filters on the database. So I decided to create some blob text fields to store those configurations that will only be used as keys to configure some modules of the application.
In this Delphi side of the application, I am using NativeXML run-time components to decode the configuration class or record type of each module into/from XML and populate that field on the database.
My problem came when I realized that this application will have a web site module where people will register for clinical attending and this part will need to use some of the configuration stored on that XML on the database. So...
I am newbie on PHP and I wish to know from you if PHP has the ability to do that XML<->Object\Record DeCoding or do I have to look for a library that makes it possible?
Note: If there is only a record type capacity, I will use it, but if not, I prefer to use classes
Edit:
In response to some comments on answers, I would say that I use XML instead of JSON because of this Delphi XML library that suited me well! If someone could point me to a goo JSON DeCode library to convert JSON<->Delphi Objects will really use it instead of XML because I like to work with JSON. Would that solve the problem on the PHP parsing?
That is not that easy in PHP. However there are lots of smart folks out there, who where facing the same problem.
Paul Ferrertt has a XML-Encode Class here:
http://www.paulferrett.com/2009/encoding-an-object-in-xml-with-php/
In PHP there are multiple functions to decode a XML sheet. Start here:
http://www.php.net/manual/en/refs.xml.php
http://php.net/manual/en/function.xml-parse.php
However you won' t be able to get an object back as easy as with json_decode() and that for a reason XML is not meant to transfer objects (and the like) around. You have to write your own conversion methods.
I suggest you to read this : http://www.php.net/manual/en/refs.xml.php. Some of these libraries are easier to use that are others, some others are more powerful, etc.

How best to pass a python dictionary to PHP using file IO

I have a python script running on a cron job, once a minute, which builds some data into a dictionary. I am writing that data into a simple, space separated, text file and reading that into PHP whenever the webpage is requested.
What is correct way to do pass this information? What I am doing now certainly feels clunky since I am rebuilding essentially the same structure in PHP. How do I just write my python dictionary (perhaps an order dictionary is needed) to disk so that PHP can read it directly into a PHP array?
I also worry a bit about whether this method of transfer is kinda archaic. Should I be using a database instead, e.g. SQLlite? I worry that someone will load the page as the file is being updated which might cause some problems.
I just need something light and simple. Don't want to overcomplicate it. Thanks in advance.
Any language can read JSON, as JSON is simply a way to format data as text, which can easily be parsed by any program. To write the dictionary as a json file, you do this:
import json
myfile = open('data.json','w+')
#create your dict
json.dump(mydict,myfile)
myfile.close()
now to read it in PHP:
$data = json_decode(file_get_contents("data.json"));
I don't know whether PHP handles json data, but if so, that's probably the way to go. See the json module documentation for more...
And in fact, PHP does handle json data: http://php.net/manual/en/book.json.php
This should be a relatively simple but robust way to transfer the data.

Extracting Data From Remote XML & Creating Wordpress Post

I've been searching for some time to figure out how to extract data from a remote XML file and then create a post automatically with the parsed XML data. I have figured out the functions to create a post using cURL/PHP but I'm not sure how to pull data from an XML file, put that data into strings and then apply those strings to a newly created post. Also dupe protection would be nice.
If anybody knows a good starting point for me to learn or has written something ealready that could provide useful assistance then that would be great. Thanks guys
PHP has a wide variety of XML parsing functions. The most popular around here is the DOM. You can use DOM functions to find the specific XML tags you're interested in and retrieve their data.
Unfortunately you did not provide an example of the XML you're trying to work with, otherwise I'd post a brief example.
If you have to stay with xml format using php you use something with this here. If you can change the format to basic csv text you could try using wordpress plugin here.
Also php has a function for csv files called fgetcsv , so I would say get the information needed from your file.
Pass to a variable and than use wp_insert_post to create a post. Put it all in a while or foreach loop and should work fine - Or try the plug-in first.
As for duplicate content, perhaps you could pass the information in an array and then use array_unique to delete any duplicates (just off the top of my head theres probably a better way or function out there).

Parsing Drupal String

I am writing my own drupal website side API.
I see that some tables containg some odd strings that i need to pharse, I cant fine any logic in it.
for example, I have upload via the admin a file and i have found the relevant row with the data.
a:12:{s:3:"alt";s:10:"aaaaaaaaaa";s:3:"fid";s:2:"22";s:5:"width";i:1024;s:6:"height";i:680;s:8:"duration";i:0;s:12:"audio_format";s:0:"";s:17:"audio_sample_rate";i:0;s:18:"audio_channel_mode";s:0:"";s:13:"audio_bitrate";i:0;s:18:"audio_bitrate_mode";s:0:"";s:4:"tags";a:0:{}s:5:"title";s:0:"";}
I need to pharse out the aaaaaaaaaa string which is the image "alt".
Do you have an idea of how to parse this string, I hope that your rule will be soutable for drupa odd strings in general?
Thanks
These strings are not Drupal-specific. They are serialized PHP data types (objects, arrays, etc.). See the PHP manual for serialize().
The string you've quoted is a PHP serialized array. See the PHP manual page for the serialize() and unserialize() functions for more info.
However I strongly doubt that you actually need to be delving into the data in this way. That data will have been set up in Drupal, and can be modified in Drupal. I can't say where, since the data you've provided is lacking context, but it'll likely be somewhere in your administration area.
I'd suggest you find where it's configured and change it there. Hacking around directly with the serialized strings that Drupal creates is a sure-fire way to damage your Drupal installation if you don't know what you're doing.
That string is serialised. You can unserialize it using http://php.net/manual/en/function.unserialize.php

Anyone able to read ning's json export files using PHP

I have a client's JSON files that he got from the NING exporter. I'm trying to load the data into PHP but seems like the json isnt properly formatted or something. SO PHP is not able to parse the JSON. I also used another PHP class to do it but that did not work either. Below is the content of one of the files
([{"id":"2492571:Note:75","contributorName":"16szgsc36qg2k","title":"Notes Home","description":"Welcome! To view all notes.","createdDate":"2008-11-14T08:44:58.821Z","updatedDate":"2008-11-14T08:44:58.821Z"}])
Help appreciated!
The parens at the beginning and end are not valid in JSON. It should parse after stripping those.
The JSON file from NING exporter is not properly formatted. For some reason, some commas are missing and you have '}{' pattern, instead of '},{' and the first and the last char is not correct.
You can write a small routine to pre-parse the file and fix those problems and some others that might appear or you can take a look at the code of this Wordpress plugin http://wordpress.org/extend/plugins/import-from-ning/ and copy the routine that fix the json file.
If you'd like to move your Ning data to another platform, you could consider Discourse. There is already an importer for it.
If you don't want to use Discourse, you can still use the (Ruby) importer source code to see how to parse the JSON file.

Categories