random-web-tools/api/php/str_to_php_array.php

38 lines
977 B
PHP
Raw Permalink Normal View History

2023-12-12 11:45:57 +02:00
<?php
2023-12-12 12:27:46 +02:00
require_once 'cors.php';
2023-12-12 11:45:57 +02:00
$input = json_decode(file_get_contents('php://input'), true);
$data = $input['data'];
$data_delimiter = $input['delimiter'];
function var_export_short($data, $return=true) {
$dump = var_export($data, true);
$dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
$dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
$dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties
if (gettype($data) == 'object') { // Deal with object states
$dump = str_replace('__set_state(array(', '__set_state([', $dump);
$dump = preg_replace('#\)\)$#', "])", $dump);
} else {
$dump = preg_replace('#\)$#', "]", $dump);
}
if ($return===true) {
return $dump;
} else {
echo $dump;
}
}
switch ($input['mode']) {
case 'json':
echo var_export_short(@json_decode($data, true));
break;
default:
echo var_export_short(explode(empty($data_delimiter) ? "\n" : $data_delimiter, $data));
break;
}