Skip to content

Instantly share code, notes, and snippets.

@adamrefaey
Last active November 16, 2022 13:41
Show Gist options
  • Save adamrefaey/81382d030a533783492c83734099e987 to your computer and use it in GitHub Desktop.
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
<?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