Last active
November 16, 2022 13:41
-
-
Save adamrefaey/81382d030a533783492c83734099e987 to your computer and use it in GitHub Desktop.
Sort env variables in a file to make it easier to compare it with another
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
<?php | |
// you should change this to be the name of the env var that you want to sort | |
$filename = 'divine'; | |
$dotenvFile = file_get_contents($filename); | |
$lines = explode(PHP_EOL, $dotenvFile); | |
$elements = []; | |
foreach ($lines as $line) { | |
if (empty($line) || ! str_contains($line, '=')) { | |
continue; | |
} | |
$lineParts = explode('=', $line, 2); | |
$key = trim($lineParts[0], ' "\''); | |
$value = trim($lineParts[1] ?? '', ' "\''); | |
$elements[$key] = $value; | |
} | |
ksort($elements); | |
$outputFile = ''; | |
foreach ($elements as $key => $value) { | |
$outputFile .= $key . '=' . '"' . $value . '"' . PHP_EOL; | |
} | |
// you should expect the output file to be the same name as the input file with _sorted appended | |
file_put_contents($filename . '_sorted', $outputFile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment