ptlis/php-serialized-data-editor

Manipulate PHP-serialized data without deserialization

0.0.2 2019-02-10 18:01 UTC

This package is auto-updated.

Last update: 2024-04-06 23:22:33 UTC


README

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