LoginSignup
4
5

More than 5 years have passed since last update.

需要は無いかもしれないが、ini形式をyaml形式に変換する処理を書いてみた。

Posted at

phpとかでini使ってたけど
rubyに引っ越したら使えないの?

いえ使えます!
使えますが、

test.ini
[section]
name=value
name2=value2

の形式のみで

test.ini
[section]
name[]=value
name[]=value2

の様に3次元配列は使えません
※調べが浅いだけで使えたら、だれか教えて下さい><

ということで、yamlなら使える
じゃあyamlに変換してみっかPHPで!ということで作ってみました。

<?php
$ini = parse_ini_file($_SERVER['argv'][1],true);
foreach($ini as $section => $conf){
    echo $section,":\n";

    //sction内ループ
    foreach($conf as $key => $val){
        if(is_array($val)){
            echo " ",$key,":\n";
            foreach($val as $num => $file){
                echo "  - ",$file,"\n";
            }
        }
        else {
            echo " ",$key,": ",$val,"\n";
        }
    }
}
?>

エラー制御とかはしてません。

test.ini
[test]
name=value
age=12
test[]=test1
test[]=test2

[test2]
name=value
age=12
test[]=test1
test[]=test2

こんなiniがコマンド一つで

php trance.php test.ini > test.yml

次の結果に

test:
 name: value
 age: 12
 test:
  - test1
  - test2
test2:
 name: value
 age: 12
 test:
  - test1
  - test2

他に便利なライブラリがあれば、そちらに切り替えます−。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5