2010-09-21 37 views
0

我有内容的.ini文件...需要解析ini文件中提取值

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

而且随着内容的文件下面是从其中通过在2个值的functions.php叫:

$ mytheme的,这是主题/模板,其颜色,正在寻求和$点,这是正在寻求(颜色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){$myColor = [insert color2 value here];} 
    if($spot == 3){$myColor = [insert color3 value here];} 
    if($spot == 4){$myColor = [insert color4 value here];} 
} 

echo $myColor; 

我在寻找特定颜色数名帮助与如何页嗨ini文件用template.ini文件中的适当颜色填充括号内的数据。

+3

你永远不要向外界使用的用户输入文件系统路径!这可能是非常危险的! – jwueller 2010-09-21 20:38:59

+1

@令人发指,很好的一点,但我认为在使用前需要对这个值进行消毒/清洁/检查会更准确。 – jeroen 2010-09-21 20:42:49

+1

@ jeroen:那就对了。对于那个很抱歉。我会在这个上使用类似['basename()'](http://php.net/basename)的东西。 – jwueller 2010-09-21 20:47:29

回答

1

使用parse_ini

$colors = parse_ini($path_to_ini, true); 

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

您不需要$点比较每个颜色 - 你可以建立数组键来获取值。