Need to parse ini file to extract values

后端 未结 3 1152
情歌与酒
情歌与酒 2021-01-25 03:58

I have a .ini file with contents of...

[template]
color1 = 000000
color2 = ff6100
color3 = ff6100
color4 = 000000

And a file with contents belo

相关标签:
3条回答
  • Use parse_ini:

    $colors = parse_ini($path_to_ini, true);
    
    if(array_key_exists($myTheme, $colors)) {
        $myColor = $colors[$myTheme]['color' . $spot];
    }
    

    You don't need to compare $spot for each color - you can build the array key to get the value.

    0 讨论(0)
  • 2021-01-25 04:42

    You can use parse_ini_file()

    0 讨论(0)
  • 2021-01-25 04:44

    There's a function for that in php: http://php.net/manual/en/function.parse-ini-file.php

    You could use it like this:

    <?php $ini_array = parse_ini_file($path_to_ini); ?>
    

    The values can be found like this:

    <?php $color1 = $ini_array['template']['color1']; ?>
    
    0 讨论(0)
提交回复
热议问题