Parsing Drupal String - php

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

Related

PHP json parser - off the shelf or roll my own?

I'm sorry if this seems like a newbie question but I am a newbie - so please be explicit in answers. I also hope that the terminology that I’m using is correct and doesn’t confuse the issue.
I need to parse a json file but I have no control over the content / structure and when the content changes the first I’ll know about it will probably be when I try to read it. My approach will be to work through the data and validate that I recognise each data item and that it’s context is as I expect, so that it’s handled correctly as I insert it into the database.
It seems to me (in my blissful ignorance) that there’s little difference in walking through an array produced by e.g. by PHP json_decode () or walking through the json, with my own specialised parser that will validate the json data as I parse it.
So my question is am I missing something here e.g. a big gotcha that's going to make parsing the json more complex than I expect or maybe I’m missing the point in some other way?
I recommend using json_decode. It will handle turning valid JSON into a PHP data structure. Then it should be much more natural to work with. Not to mention there are probably a lot of things you could get wrong in the parser that you won't have to worry about because json_decode will handle it.
Once you've got a PHP data structure, then work with that. Do your validation etc. there.
I'm a bit confused as to what your question is. I would recommend sticking with json_decode() and not to write your own parser as your's would probably end up simulating json_decode() anyways.
You can validate your json values as you move down each propery however you like.

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.

What language or format is this in?

After looking at the Wordpress and Joomla database, I began to recognize strings in this format. What is it, how does it work?
here's an example:
a:5:{s:13:"administrator";a:2:{s:4:"name";s:13:"Administrator";s:12:"capabilities";a:62:{s:13:"switch_themes";b:1;s:11:"edit_themes";b:1;
that was serialized string from an object or array.
It's a serialized array. Basically a string to be used by the serialize() and unserialize() functions in PHP, the base language for wordpress and likely joomla though I have never worked with joomla myself.

Rendering a page in PHP: How?

This may be a inappropriate question for SO, but I thought lets see :)
I'm writing a website in php. Every pageload may have 10-20 DB requests.
Using the result of the DB queries I need to generate a page.
The page would contain a topic (should be image or text) followed by comments. There could be mutiple topics like this.
Currently, I'm creating a string using the DB result and sending it to the browser.
When browser receives the string (as an ajax response), it parses using split functions and creates the HTML dynamically.
I'm basically a C++ programmer; relatively new to web development. So, I do not have fair understanding of the JS objects. How long of a string can JS variable hold? Is it ok to use split and generate HTML at the client.
I'm not generating the complete HTML at the server side to avoid any overhead because of string concatenation. I believe sending less no. of characters to the client (like I'm doing) is better as compared to sending complete HTML code.
Is something (or everything) wrong in my understanding :)
Any help is appreciated.
EDIT:
Well, I'll be highly grateful if I could get opinions in yes/no. What would you recommend. Sending HTML to the client or a string that will be used at the client to generate HTML?
Unless you have a specific reason for doing so, I think you should look into generating the HTML with PHP and sending it directly to the browser. PHP was built specifically for this purpose.
I think you be best off to look at jQuery and more specific to the AJAX method of that library. Also, take a look at JSON and you should be all good to go.
Have you considered using a templating engine like Smarty?
It's pretty simple to use, take a look at the crash course, you might like it! http://www.smarty.net/crash_course

Is JSON.stringify() reliable for serializing JSON objects?

I need to send full objects from Javascript to PHP. It seemed pretty obvious to do JSON.stringify() and then json_decode() on the PHP end, but will this allow for strings with ":" and ","? Do I need to run an escape() function on big user input strings that may cause an issue? What would that escape function be? I don't think escape works for my purposes.
Are there any downsides to JSON.stringify() I need to know about?
Thanks
Yes, it's reliable in any decent implementation (like Crockford's), and no, you don't have to run it through escape first (if you do that, PHP will be pretty confused at the other end). Browsers are starting to get their own implementations of JSON stuff (now that it's in the 5th edition spec), but for now, you may be best off using Crockford's or similar.
There is a pretty good description of what JSON.stringify() does here:
http://www.json.org/js.html
The source code is also available if you want to be certain and/or make changes.
I have been using it for months without problem.
Also,
I'm not sure if you have seen the man page for json_decode, lots of good info there aswell:
http://ie2.php.net/manual/en/function.json-decode.php
HTH

Categories