This library provides six different handlers for reversing output of PHP's print_r function back to original variables.
If there's no handler available for a type it's returned as string.
string"") is treated as null (see NullHandler)boolean support)array MUST be supported with type-castingpublic, protected and private properties of Objects MUST be supported with type-castingPackage is available via Composer.
composer require simivar/reverse-print-r
<?php
$print_r_output = print_r([
'string' => 'some text',
'integer' => 1,
'float' => 2.3,
'subArray' => [
'Hello World.',
],
], true);
$reverser = new ReversePrintRReversePrintR($print_r_output);
echo $reverser->reverse()['float'];
// outputs "2.3"All handlers are defined as final, but thanks to Dependency Injection it's easy to change the behavior of library
and it's type-casting. Let's say you want to keep all the empty strings "" as string, not null. All you have to do
is create your own HandlerRunner without NullHandler.
<?php
$print_r_output = print_r([
'string' => '',
'null' => null,
], true);
$handlerRunner = new ReversePrintRHandlerRunner(
new ReversePrintRHandlerFloatHandler(),
new ReversePrintRHandlerIntegerHandler(),
new ReversePrintRHandlerArrayHandler(),
new ReversePrintRHandlerObjectHandler()
);
$reverser = new ReversePrintRReversePrintR($print_r_output, $handlerRunner);
var_dump($reverser->reverse()['null']);
// outputs ""The same way to removed NullHandler you can add your own handlers. All you have to do is make sure that it implements
ReversePrintRHandlerHandlerInterface and you are good to go.
Library is following Semver. All minor and patch updates are backwards compatible.
Please see the license file for more information.