Created
October 24, 2017 06:00
-
-
Save Enelar/fc334e4f787a056c823ab8615ae9d470 to your computer and use it in GitHub Desktop.
Parse PostgreSQL multidimensional array string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function RecursiveParse($text) | |
{ | |
if (is_array($text)) | |
{ | |
$ret = []; | |
foreach ($text as $row) | |
$ret[] = RecursiveParse($row); | |
return $ret; | |
} | |
echo "START"; | |
var_dump($text); | |
$split = explode(',', $text); | |
$values = []; | |
$brackets = null; | |
$quotes = []; | |
$scope = &$brackets; | |
var_dump($split); | |
foreach ($split as $value) | |
{ | |
var_dump(["ITER", "values" => $values, "value" => $value, "brackets" => $brackets, "quotes" => $quotes, "scope" => $scope]); | |
if (count($quotes) || $value[0] == '"') | |
$quotes[] = $value; | |
var_dump("RILI", count($quotes)); | |
if (count($quotes)) | |
{ | |
if ($value == '"') | |
continue; | |
if (substr($value, -1) != '"') | |
continue; | |
if (substr($value, -2) == '\\"') | |
continue; | |
// End of string | |
$value = implode(',', $quotes); | |
$quotes = []; | |
if ($brackets !== null) | |
{ | |
$scope[] = $value; | |
continue; | |
} | |
} | |
if ($value[0] == '{') | |
{ | |
$cut = '{' . $value; | |
var_dump(["CUT", $cut]); | |
while (strlen($cut) > 1) | |
{ | |
if ($cut[0] != '{') | |
break; | |
$cut = substr($cut, 1); | |
if ($cut[0] != '{') | |
{ | |
echo "PUT"; | |
echo $cut; | |
var_dump($values, $scope); | |
if ($cut[0] == '"') | |
{ | |
echo "wat"; | |
var_dump($scope); | |
if (!preg_match('/^\"(.*)\"}*$/', $cut, $match)) | |
{ | |
$quotes[] = $cut; | |
echo "quotes"; | |
continue; | |
} | |
else | |
$scope[] = $cut; | |
var_dump($scope, $match); | |
} | |
else | |
$scope[] = $cut; | |
echo "DONE"; | |
} | |
else | |
{ | |
var_dump($scope); | |
if ($scope === null) | |
{ | |
$scope = []; | |
continue; | |
} | |
echo "EXTRA"; | |
$scope[] = []; | |
var_dump($brackets); | |
end($scope); | |
$saved = &$scope[key($scope)]; | |
unset($scope); | |
$scope = &$saved; | |
var_dump($brackets); | |
} | |
} | |
if (count($quotes)) | |
continue; | |
} | |
else if (count($scope)) | |
$scope[] = $value; | |
var_dump($brackets); | |
if (count($brackets)) | |
{ | |
if (substr($value, -1) == '}') | |
{ | |
$cut = $value; | |
var_dump($value); | |
while (strlen($cut) > 1) | |
{ | |
$ch = substr($cut, -1); | |
$cut = substr($cut, 0, -1); | |
if ($ch != '}') | |
break; | |
end($scope); | |
$last = &$scope[key($scope)]; | |
$last = substr($last, 0, -1); | |
if (preg_match('/^\"(.*)\"(}*)$/', $last, $match)) | |
$last = $match[1] . $match[2]; | |
if ($scope === $brackets) | |
{ | |
echo "FOLDING"; | |
$values[] = $brackets; | |
$scope = null; | |
break; | |
} | |
var_dump(['add', $brackets, $scope]); | |
$saved_stack = $scope; | |
$scope = null; | |
var_dump($brackets); | |
$latest_scope = &$brackets; | |
while (is_array(end($latest_scope))) | |
{ | |
var_dump(["wtf", end($latest_scope)]); | |
$php_wtf = &$latest_scope[key($latest_scope)]; | |
unset($latest_scope); | |
$latest_scope = &$php_wtf; | |
} | |
var_dump(["pre", $brackets, $saved_stack, $latest_scope]); | |
// $pointer = &$latest_scope[key($latest_scope)]; | |
if ($scope !== $brackets) | |
{ | |
$scope = '{' . implode(',', $saved_stack) . '}'; | |
unset($scope); | |
$scope = &$latest_scope; | |
} | |
else | |
{ | |
var_dump($saved_stack); | |
} | |
var_dump([$brackets, $saved_stack, $latest_scope]); | |
var_dump(["res", $brackets, $scope]); | |
} | |
if ($brackets !== $scope && $scope !== null) | |
continue; | |
echo "TODO FOLD TO VALUES"; | |
var_dump($brackets); | |
var_dump($values); | |
continue; | |
var_dump($scope); | |
$scope = null; | |
} | |
continue; | |
} | |
$values[] = $value; | |
} | |
echo "RETURNING"; | |
var_dump($values); | |
if (is_string($values[0])) | |
$return = $values[0]; | |
else | |
$return = RecursiveParse($values[0]); | |
var_dump($return); | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment