2010-09-21 38 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]; 
} 

您不需要$點比較每個顏色 - 你可以建立數組鍵來獲取值。