Add php array to json tool
This commit is contained in:
58
api/php/str_to_php_array.php
Normal file
58
api/php/str_to_php_array.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
// Allow from any origin
|
||||
if (isset($_SERVER['HTTP_ORIGIN'])) {
|
||||
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
|
||||
// you want to allow, and if so:
|
||||
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
|
||||
header('Access-Control-Allow-Credentials: true');
|
||||
header('Access-Control-Max-Age: 86400'); // cache for 1 day
|
||||
}
|
||||
|
||||
// Access-Control headers are received during OPTIONS requests
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
||||
|
||||
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
|
||||
// may also be using PUT, PATCH, HEAD etc
|
||||
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
|
||||
|
||||
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
|
||||
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
Reference in New Issue
Block a user