PHPackages                             academe/serializeparser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. academe/serializeparser

ActivePackage[Parsing &amp; Serialization](/categories/parsing)

academe/serializeparser
=======================

A PHP parser for serialized data, to be able to 'peek' into serialize strings.

1.0.2(9y ago)490.6k↓11.6%2[4 issues](https://github.com/academe/SerializeParser/issues)[1 PRs](https://github.com/academe/SerializeParser/pulls)MITPHPPHP &gt;=5.4.0

Since Jul 4Pushed 9y ago2 watchersCompare

[ Source](https://github.com/academe/SerializeParser)[ Packagist](https://packagist.org/packages/academe/serializeparser)[ Docs](https://github.com/academe/SerializeParser)[ RSS](/packages/academe-serializeparser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

[![Build Status](https://camo.githubusercontent.com/c9cb715d208fba4ebefb04efa19946c3134462ffe74e595f940dba46776c48e9/68747470733a2f2f7472617669732d63692e6f72672f61636164656d652f53657269616c697a655061727365722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/academe/SerializeParser)[![Latest Stable Version](https://camo.githubusercontent.com/ce3154c86a17588cb95d2e02fc973187cb88f3e62653d69a351b1975735efdf8/68747470733a2f2f706f7365722e707567782e6f72672f61636164656d652f53657269616c697a655061727365722f76657273696f6e2e706e67)](https://packagist.org/packages/academe/SerializeParser)[![Total Downloads](https://camo.githubusercontent.com/343a88cad6929de8a79600318a1e11fd0eeff724971411b8f2adbd97e0e77ab7/68747470733a2f2f706f7365722e707567782e6f72672f61636164656d652f53657269616c697a655061727365722f642f746f74616c2e706e67)](https://packagist.org/packages/academe/SerializeParser)

SerializeParser
===============

[](#serializeparser)

A PHP parser for serialized data, to be able to "peek" into serialized strings.

Purpose
-------

[](#purpose)

I had a bunch of PHP serialized data in the database, and needed to inspect that data for presenting information to the administrator. There appeared to be no way to interpret this data in PHP without instantiating all the objects that were in that data, and I did not want to do that, and did not have all the classes handy anyway.

There is a setting that will allow you to replace *missing* classes with another class, but that would still mean instantiating those classes that *could* be found.

So here we are, a simple parser that takes a serialized data string, and tries to deserialize it, but replacing all objects with a `stdClass` so we are not instantiating classes that have no business being instantiated, or classes that may not even exist in the application.

How to Use
----------

[](#how-to-use)

Here is a simple example:

```
// Create a complex array/object/string/number/boolean to serialize.

$obj = new \Academe\SerializeParser\StringReader('xyz');
$obj->foo = true;

$data = [
    'a' => 1,
    [
        'foo' => 'bar',
        $obj,
        'b' => false,
    ],
];

$serialized = serialize($data);

// Take a look a how PHP has serialised it.

echo $serialized;
// a:3:{s:1:"a";i:1;i:0;a:3:{s:3:"foo";s:3:"bar";i:0;O:36:"Academe\SerializeParser\StringReader":4:{s:6:"*pos";i:0;s:6:"*max";i:2;s:9:"*string";s:3:"xyz";s:3:"foo";b:1;}i:1;O:7:"myClass":2:{s:12:"*protected";s:4:"prot";s:16:"myClassprivate";s:4:"priv";}}s:1:"b";b:0;}

// Somewhat beautified:

/*
a:3:{
    s:1:"a";i:1;
    i:0;a:3:{
        s:3:"foo";s:3:"bar";
        i:0;O:36:"Academe\SerializeParser\StringReader":4:{
            s:6:"*pos";i:0;
            s:6:"*max";i:2;
            s:9:"*string";s:3:"xyz";
            s:3:"foo";b:1;
        }
        i:1;O:7:"myClass":2:{
            s:12:"*protected";s:4:"prot";
            s:16:"myClassprivate";s:4:"priv";
        }
    }
    s:1:"b";b:0;
}
*/

// Now parse it to look at what it inside, without instantiating the
// original objects in it.

$parser = new \Academe\SerializeParser\Parser;
$parsed = $parser->parse($serialized);

var_dump($parsed);

/*
array(3) {
  ["a"]=>
  int(1)
  [0]=>
  array(3) {
    ["foo"]=>
    string(3) "bar"
    [0]=>
    object(stdClass)#6 (5) {
      ["__class_name"]=>
      string(36) "Academe\SerializeParser\StringReader"
      ["pos"]=>
      int(0)
      ["max"]=>
      int(2)
      ["string"]=>
      string(3) "xyz"
      ["foo"]=>
      bool(true)
    }
    [1]=>
    object(stdClass)#7 (3) {
      ["__class_name"]=>
      string(7) "myClass"
      ["protected"]=>
      string(4) "prot"
      ["private"]=>
      string(4) "priv"
    }
  }
  ["b"]=>
  bool(false)
}
*/
```

Note that the `StringReader` class has been unserialized as `stdClass` and the original name moved to attribute `__class_name`. The protected and private attributes are all also present and accessible (though the final parsed structure does not show which were protected or private - maybe a metadata attribute could list those?).

Remember, the purpose of this is not to reconstruct the original data as an accurate representation. It is to allow the data to be inspected and some key values pulled out for logging, showing to the user etc.

TODO
----

[](#todo)

- Maybe make the `Parser::parse()` method static.
- Make the StringReader a little more efficient. Efficiency was not key in getting this working.
- Inject the string reader so it can be mocked for testing.

Want to Help?
-------------

[](#want-to-help)

If you fancy writing some tests, have found a bug, or can extend it to handle more cases of serialized data, then please feel free to get involved. PRs, issues, or just email me - whatever you like.

Source Specification
--------------------

[](#source-specification)

The only source specification for how serialization works, is the PHP source code. However, there are a number of serialized parsers written for langauegs other than PHP that work, and have been derived from that code. I have ported this code from some of those packages.

This is not complete, so will not handle references for example, but does enough of the simple stuff for my needs.

- This has a handy algorithm in Python:
    [https://github.com/jqr/php-serialize/blob/master/lib/php\_serialize.rb#L195](https://github.com/jqr/php-serialize/blob/master/lib/php_serialize.rb#L195)
- There is a pretty good descriptino of some of the intricaces here:

- My origional SO question that led me to write my own solution:

- This excerpt from the PHP Internals book gives a great breakdown of all of the data types:
    [http://www.phpinternalsbook.com/classes\_objects/serialization.html](http://www.phpinternalsbook.com/classes_objects/serialization.html)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 88.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~301 days

Total

3

Last Release

3368d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/395934?v=4)[Jason Judge](/maintainers/judgej)[@judgej](https://github.com/judgej)

---

Top Contributors

[![judgej](https://avatars.githubusercontent.com/u/395934?v=4)](https://github.com/judgej "judgej (31 commits)")[![jj5](https://avatars.githubusercontent.com/u/5469039?v=4)](https://github.com/jj5 "jj5 (4 commits)")

---

Tags

phpserializeunserialize

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/academe-serializeparser/health.svg)

```
[![Health](https://phpackages.com/badges/academe-serializeparser/health.svg)](https://phpackages.com/packages/academe-serializeparser)
```

###  Alternatives

[zumba/json-serializer

Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.

129743.7k13](/packages/zumba-json-serializer)[corveda/php-sandbox

A PHP library that can be used to run PHP code in a sandboxed environment

23483.5k2](/packages/corveda-php-sandbox)[bupy7/xml-constructor

The array-like constructor of XML document structure.

1337.9k](/packages/bupy7-xml-constructor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
