The PHP Serializer is a python module that simulates the (un)serialize functions known in PHP. It enables a complex data exchange between PHP and Python scripts on a lower level without the use of any heavy weight libraries.
The module is completely written in Python and follows the PHP serialize definition.
The usage is very simple and intuitive:
#!/usr/bin/python
# data is serialized data provided by php
from PHPSerializer import *
try:
tmp = PHPSerializer.unserialize(data)
tmp['a'] = "hello python"
tmp['b'] = [5,6,7,8]
tmp['c']['orange'] = ["a", "b", "c", "d"]
print PHPSerializer.serialize(tmp)
except Exception, e:
print e
PHP Serializer is a Python module and needs a interpreter running. It has been tested under Python 2.2 and 2.3.
You can download the latest versions of the project here:
The PHP Serializer comes as a module and can be easily used by importing it into your script with an import statement.
The module is written as a class an provides two static methods for functionality. No instantiation is needed.
The PHP Serializer class itself turns the input types into their corresponding string representation based on an very simple mapping.
| Python | PHP | serialized |
|---|---|---|
| none | NULL | N; |
| bool | bool | b:1; | b:0; |
| long | float | double | d:$data; |
| int | int | i:$data; |
| string | string | s:$length:$data; |
| tuple | list | dict | array | a:$count:{(i|s):$key;(i|s|a):value}; |
To serialize data, simply call the static serialize method:
PHPSerializer.serialize(data).
To unserialize data, simply call the static unserialize method:
PHPSerializer.unserialize(data).
This library is free software; you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more information visit http://www.gnu.org/licenses/gpl.txt.
For questions or suggestions drop a line to info@flamelab.de.
0.3: (2005-10-06) initial release
copyright 2007 by flamelab.de