lbreme/lexepa-srl

用于在PHP中对序列化字符串进行词法和解析的类

v1.0.3 2021-05-19 21:52 UTC

This package is auto-updated.

Last update: 2024-09-20 05:51:30 UTC


README

PHP中序列化字符串的词法和解析库。

安装Lexepa-Srl

首先,如果你还没有使用它,请获取Composer

接下来,在你的项目目录中运行以下命令

composer require lbreme/lexepa-srl

它是如何工作的?

Lexepa-Srl库分析来自PHP序列化结果的任何文本字符串。在分析过程中,会调用一系列回调函数,并将序列化字符串的构成元素作为参数传递给这些函数。

以下是一个示例,包含在文件class-example-srl.php中,要使其工作,请将其复制到项目的根目录下

/*
We create a class derived from the Lexepa_Srl_Abstract class, which implements all the
callback functions that will be called by the analysis of the serialized string
*/
class Example_Srl extends Lexepa_Srl_Abstract
{
	/**
	 * Begin of parsing
	 *
	 * @param string $string Original string to unserialize.
	 * @param int    $offset Initial offset.
	 */
	public function begin_parsing( $string, $offset ) {
		echo 'String serialized: ' . $string . '<br />';
	}

	/**
	 * String length value found
	 *
	 * @param string $string_length String length value.
	 * @param int    $offset Offset of the string length value found.
	 */
	public function string_length( $string_length, $offset )
	{
		echo 'String length: ' . $string_length . '<br />';
	}

	/**
	 * String value found
	 *
	 * @param string $string_value String value.
	 * @param int    $offset Offset of the string value found.
	 */
	public function string_value( $string_value, $offset )
	{
		echo 'String value: ' . $string_value . '<br />';
	}

	/**
	 * Integer value found
	 *
	 * @param string $integer_value Integer value.
	 * @param int    $offset Offset of the integer value found.
	 */
	public function integer_value( $integer_value, $offset )
	{
		echo 'Integer value: ' . $integer_value . '<br />';
	}

	/**
	 * Decimal value found
	 *
	 * @param string $decimal_value Decimal value.
	 * @param int    $offset Offset of the decimal value found.
	 */
	public function decimal_value( $decimal_value, $offset ) {
		echo 'Decimal value: ' . $decimal_value . '<br />';
	}

	/**
	 * Boolean value found
	 *
	 * @param string $boolean_value It can be '0' or '1'.
	 * @param int    $offset Offset of the boolean value found.
	 */
	public function boolean_value( $boolean_value, $offset ) {
		echo 'Boolean value: ' . $boolean_value . '<br />';
	}

	/**
	 * Null value found
	 *
	 */
	public function null_value() {
		echo 'Null value found' . '<br />';
	}

	/**
	 * Number of items of the array found
	 *
	 * @param string $items_num Number of items.
	 * @param int    $offset Offset of the number of items found.
	 */
	public function array_items_num( $items_num, $offset ) {
		echo 'Number of items of the array: ' . $items_num . '<br />';
	}

	/**
	 * End of parsing
	 *
	 * @param bool $parsing_result True if the string is unserializable.
	 */
	public function end_parsing( $parse_result )
	{
		if ( $parse_result ) {
			echo 'Good job!' . '<br />';
		} else {
			echo 'There was an error' . '<br />';
		}
	}

	/**
	 * Set error parsing the string.
	 *
	 * @param string $error Error parsing the string.
	 */
	public function set_error( $error )
	{
		echo $error . '<br />';
	}
}

$example_srl = new Example_Srl();

/*
We instantiate the Lexepa-Srl library class, passing as arguments the $example_srl object
containing the callback functions and the serialized string
*/
$lexepa_srl  = new Lexepa_Srl( $example_srl, $myArraySerialized );

// Let's start the analysis
$lexepa_srl->parse_srl();

此示例的结果如下

String serialized: a:5:{s:4:"key1";s:22:"This is my first value";s:4:"key2";s:23:"This is my second value";s:4:"key3";i:20;s:4:"key4";b:1;s:4:"key5";N;}
Number of items of the array: 5
String length: 4
String value: key1
String length: 22
String value: This is my first value
String length: 4
String value: key2
String length: 23
String value: This is my second value
String length: 4
String value: key3
Integer value: 20
String length: 4
String value: key4
Boolean value: 1
String length: 4
String value: key5
Null value found
Good job!

Lexepa_Srl_Abstract类实现的回调函数包含并记录在接口文件class-lexepa-srl-interface.php

Lexepa_Srl库是根据以下文档创建的,该文档指定了在PHP中对象序列化的方式

https://www.phpinternalsbook.com/php5/classes_objects/serialization.html