开发者

Need to parse ini file to extract values

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

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

And a file with contents below which is called from functions.php which passes in 2 values:

$myTheme, which is the name of the theme/template whose colors are being sought and $spot, which is the specific color number being sought (colors 1-4)

$myTheme = $_REQUEST['theme'];
$spot = $_REQUEST['spot'];
$myColor = get_option($myTheme);

    $path_to_ini = "styles/". $myTheme . "/template.ini";

if ($myColor == "") {
     if($spot == 1){$myColor = [insert color1 value here];}
     if($spot == 2){$my开发者_C百科Color = [insert color2 value here];}
     if($spot == 3){$myColor = [insert color3 value here];}
     if($spot == 4){$myColor = [insert color4 value here];}
}

echo $myColor;

I'm looking for help with how to parse the ini file to fill in the bracketed data with the appropriate color from the template.ini file.


You can use parse_ini_file()


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']; ?>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜