Created
July 22, 2021 14:35
-
-
Save dj1020/94d4cd2a770b66b8601dee1aac7605d8 to your computer and use it in GitHub Desktop.
Copy JSON string and parse to PHP Array
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
// Under Mac system, if you have pbcopy, pbpaste | |
// 如果你用 Mac, 有 pbcopy, pbpaste。就可以直接複製 json 後到 shell 下指令,會直接變成 PHP array 後直接在剪貼簿可以貼上。 | |
alias jsontoarray='pbpaste | jsonToPhpArray | pbcopy' |
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
#!/usr/bin/env php | |
<?php | |
function varexport($expression, $return=FALSE) { | |
$export = var_export($expression, TRUE); | |
$export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export); | |
$array = preg_split("/\r\n|\n|\r/", $export); | |
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => ['], $array); | |
$export = join(PHP_EOL, array_filter(["["] + $array)); | |
if ((bool)$return) return $export; else echo $export; | |
} // https://gist.github.com/Bogdaan/ffa287f77568fcbb4cffa0082e954022 | |
// echo "Parse JSON from stdin to PHP short array..." . PHP_EOL; | |
fwrite(STDERR, "Parse JSON from stdin to PHP short array..." . PHP_EOL . PHP_EOL); | |
$content = file_get_contents("php://stdin"); | |
$json = json_decode($content, true); | |
$prettyContent = json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); | |
fwrite(STDERR, $prettyContent . PHP_EOL); | |
varexport($json); | |
fwrite(STDERR, PHP_EOL . 'Done.' . PHP_EOL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment