ptlis/php-serialized-data-editor

在不反序列化的情况下操作PHP序列化数据

0.0.2 2019-02-10 18:01 UTC

This package is auto-updated.

Last update: 2024-09-07 00:07:16 UTC


README

用于在不反序列化和重新序列化的情况下操作PHP序列化数据结构的工具。

这在以下情况下很有用

  • 你没有或不能加载类定义(例如,当在数据库、Redis等中处理任意数据时)。
  • 你不想调用__wakeup方法(例如,它试图连接到执行上下文中不可用的资源)。

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version

安装

使用composer

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

用法

此库目前支持基本搜索和替换字符串值(忽略数组键和属性名)

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