Skip to content

ptlis/php-serialized-data-editor

Repository files navigation

ptlis/php-serialized-data-editor

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 __wakeup method (e.g. it tries to connect to a resource not available in the execution context).

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version

Install

With composer:

$ composer require ptlis/php-serialized-data-editor

Usage

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'
 * ]
 */

About

Manipulate PHP-serialized data without deserialization

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages