Manipulate PHP-serialized data without deserialization
Tools for manipulating PHP serialized datastructures without deserializing and re-serializing the data.
This is useful when:
- You don't or can't have the classe definitions loaded (e.g. when processing arbitrary data in a DB, Redis etc).
- You don't want to invoke the
__wakeupmethod (e.g. it tries to connect to a resource not available in the execution context).
$ composer require ptlis/php-serialized-data-editor
This library currently supports basic search & replacement of string values (ignoring array keys and property names):
use ptlis\SerializedDataEditor\Editor; // Mock some serialized data $serialized = serialize([ 'test' => 'foo', 'test2' => 'foobar foo', 'foo' => 'baz' ]); $editor = new Editor(); // Count how many times the search term exists as a string value $containsCount = $editor->containsCount($serialized, 'foo'); // $containsCount === 3 // Replace all instances of the search term with the replacement term $modified = $editor->replace($serialized, 'foo', 'wibble'); /** * $modified when unserialized is: * [ * 'test' => 'wibble', * 'test2' => 'wibblebar wibble', * 'foo' => 'baz' * ] */