Skip to content

Instantly share code, notes, and snippets.

@Kris-Driv
Last active December 8, 2016 16:56
Show Gist options
  • Save Kris-Driv/aa5bd1db35187093f614f2d91e44376e to your computer and use it in GitHub Desktop.
Save Kris-Driv/aa5bd1db35187093f614f2d91e44376e to your computer and use it in GitHub Desktop.
<?php
// Prepare
if(!function_exists('yaml_emit')) die('yaml extension not loaded');
if(!isset($argv[1])) die("enter a valid path to file".PHP_EOL);
$source = $argv[1];
if(!file_exists($source)) die("file '$source' does not exist".PHP_EOL);
$to = substr($source, 0, strpos($source, '.ini')) . ".yml";
if(file_exists($to)) die("can't save file to '$to' file already exists".PHP_EOL);
// Read ini
$data = [];
$lines = explode(PHP_EOL, file_get_contents($source));
try {
foreach($lines as $line) {
if(strlen($line) <= 0) continue;
if($line{0} === "#") continue;
$kv = explode("=", $line);
$data[$kv[0]] = implode("=", array_slice($kv, 1));
}
} catch(\Exception $e) {
die("Error while parsing ini file: " . $e->getMessage().PHP_EOL);
}
// Save
file_put_contents($to, yaml_emit($data));
echo "YAML File saved to $to!".PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment