Skip to content

Instantly share code, notes, and snippets.

@jesseschalken
Last active June 22, 2024 20:44
Show Gist options
  • Save jesseschalken/c4abddcb2fd7051389a4 to your computer and use it in GitHub Desktop.
Save jesseschalken/c4abddcb2fd7051389a4 to your computer and use it in GitHub Desktop.
Pretty print a PHP serialized value. Useful for finding the differences between two serialized values.
<?php
/*
MIT License
Copyright (c) 2021 Jesse Schalken
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class FormatSerialize {
/**
* Pretty print a string in PHP's serialization format.
* @param string $s
* @return string
*/
public static function format($s) {
$self = new self($s);
return $self->parse();
}
private $s, $p = 0;
private function __construct($s) {
$this->s = $s;
}
private function read($n = 1) {
$r = substr($this->s, $this->p, $n);
$this->p += $n;
return $r;
}
private function readTo($s) {
$p = strpos($this->s, $s, $this->p);
if ($p === false)
throw new \Exception("Could not find '$s' from offset $this->p in '$this->s'");
return $this->read($p - $this->p);
}
private function assert($s) {
$p = $this->p;
$r = $this->read(strlen($s));
if ($r !== $s)
throw new Exception("Got '$r' but expected '$s' at offset $p in '$this->s'");
return $r;
}
private function parseArray($nl) {
$s = '';
$s .= $this->assert(':');
$s .= ($n = $this->readTo(':'));
$s .= $this->assert(':{') . $nl;
for ($i = 0; $i < $n; $i++) {
$k = $this->parse("$nl ");
$v = $this->parse("$nl ");
$s .= " $k $v$nl";
}
return $s . $this->assert('}');
}
private function parseString() {
$s = '';
$s .= $this->assert(':');
$s .= ($l = $this->readTo(':'));
$s .= $this->assert(':"');
$s .= $this->read($l);
$s .= $this->assert('"');
return $s;
}
private function parse($nl = "\n") {
switch ($s = $this->read()) {
case 'N':
$s .= $this->assert(';');
return $s;
case 'o':
case 'O':
$s .= $this->parseString();
$s .= $this->parseArray($nl);
return $s;
case 's':
$s .= $this->parseString();
$s .= $this->assert(';');
return $s;
case 'a':
return $s . $this->parseArray($nl);
case 'C':
$s .= $this->parseString();
$s .= $this->assert(':');
$s .= ($l = $this->readTo(':'));
$s .= $this->assert(':{');
$s .= $this->read($l);
$s .= $this->assert('}');
return $s;
default:
$s .= $this->assert(':');
$s .= $this->readTo(';');
$s .= $this->assert(';');
return $s;
}
}
}
class Foo {
public $foo = 9;
protected $blah;
private $baz;
function __construct() {
$this->baz = array(
'',
true,
array(
871234.12398,
serialize(array('a serialized string in a serialized string')),
),
);
}
}
print FormatSerialize::format(serialize(new Foo));
/*
O:3:"Foo":3:{
s:3:"foo"; i:9;
s:7:"*blah"; N;
s:8:"Foobaz"; a:3:{
i:0; s:0:"";
i:1; b:1;
i:2; a:2:{
i:0; d:871234.12398000003;
i:1; s:60:"a:1:{i:0;s:42:"a serialized string in a serialized string";}";
}
}
}
*/
@jesseschalken
Copy link
Author

Hi @williamdes. Thanks for asking. I have just added a license header. I hope this is permissive enough for your use.

@williamdes
Copy link

williamdes commented May 19, 2021

Hi @williamdes. Thanks for asking. I have just added a license header. I hope this is permissive enough for your use.

Hi @jesseschalken

This is perfect !
Finally I removed my comment because I had forgot that we need to reverse the operation (unserialize()).
But I (or someone else) could use your code someday ;)

We are now using https://github.com/zumba/json-serializer that allows to make the process work both ways

@jesseschalken
Copy link
Author

@williamdes No problem, I hope someone else finds it useful.

@guelzow
Copy link

guelzow commented Jan 12, 2022

Hi @jesseschalken

Thank you for this gist, it works like a charm.

I want to use it in two of our open source projects.
So . . . is this ok?

Tobi

@jesseschalken
Copy link
Author

@guelzow Sure, that's what the license header is for. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment