2011-11-29 94 views
0

我創建了一個類似於drupal中的.info文件。用PHP閱讀.info文件

#Comment 
Template Name = Valley 

styles[] = styles/styles.css, styles/media.css 
scripts[] = js/script.js 

我想用PHP獲取每個變量及其值。例如,我想將Template Name的值放到一個名爲Template Name的PHP變量中,並且如果存在一個值,則將styles[]的值放入一個數組中。

我還需要避免它在文本前定義爲散列#的評論。

這似乎很多問,bt我真的不知道如何去做這件事。如果有人有解決方案,我會非常感激,但是如果有人能夠指出我的方向是正確的,那將會很有幫助。

感謝先進!

+0

看起來像'.ini'文件格式。那是Drupal用來讀取它的機會嗎? – mario

+0

我已經創建了一個函數,這裏是我輸入到函數 - 'split_sheets(strstr($ doc,「styles []」))''。 '$ doc'是一個'fread'。然後該函數用'explode'分割字符串兩次,第一次搜索一個'='分裂,並用''爆炸搜索','。這對一條線有效,但是當我加入時,它們彼此衝突。 – PapaSmurf

+0

我不確定drupal如何讀取它。 – PapaSmurf

回答

1

如果你可以稍微adkust您的信息的文件,你可以使用內置的PHP函數:

http://php.net/manual/en/function.parse-ini-file.php

#Comment 
TemplateName = Valley 

styles[] = "styles/styles.css" 
styles[] = "styles/media.css" 
scripts[] = "js/script.js" 

這將導致數組

+0

這可能是最好的做法!我會先給它並報告,謝謝! – PapaSmurf

+0

這已經成功了,謝謝! – PapaSmurf

0

的Drupal是一個很好的提示:

function drupal_parse_info_file($filename) { 
    $info = array(); 
    $constants = get_defined_constants(); 

    if (!file_exists($filename)) { 
    return $info; 
    } 

    $data = file_get_contents($filename); 
    if (preg_match_all(' 
    @^\s*       # Start at the beginning of a line, ignoring leading whitespace 
    ((?: 
     [^=;\[\]]|     # Key names cannot contain equal signs, semi-colons or square brackets, 
     \[[^\[\]]*\]     # unless they are balanced and not nested 
    )+?) 
    \s*=\s*       # Key/value pairs are separated by equal signs (ignoring white-space) 
    (?: 
     ("(?:[^"]|(?<=\\\\)")*")|  # Double-quoted string, which may contain slash-escaped quotes/slashes 
     (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes 
     ([^\r\n]*?)     # Non-quoted string 
    )\s*$       # Stop at the next end of a line, ignoring trailing whitespace 
    @msx', $data, $matches, PREG_SET_ORDER)) { 
    foreach ($matches as $match) { 
     // Fetch the key and value string 
     $i = 0; 
     foreach (array('key', 'value1', 'value2', 'value3') as $var) { 
     $$var = isset($match[++$i]) ? $match[$i] : ''; 
     } 
     $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3; 

     // Parse array syntax 
     $keys = preg_split('/\]?\[/', rtrim($key, ']')); 
     $last = array_pop($keys); 
     $parent = &$info; 

     // Create nested arrays 
     foreach ($keys as $key) { 
     if ($key == '') { 
      $key = count($parent); 
     } 
     if (!isset($parent[$key]) || !is_array($parent[$key])) { 
      $parent[$key] = array(); 
     } 
     $parent = &$parent[$key]; 
     } 

     // Handle PHP constants. 
     if (isset($constants[$value])) { 
     $value = $constants[$value]; 
     } 

     // Insert actual value 
     if ($last == '') { 
     $last = count($parent); 
     } 
     $parent[$last] = $value; 
    } 
    } 

    return $info; 
} 

Source,這個功能是Drupal的代碼庫的一部分,drupal's license適用,用於這裏只是文檔目的。

+0

謝謝你,我想我只是使用PHP的解析ini函數。這可能對別人有用,謝謝! – PapaSmurf

1

如果你是之後是「類似」的東西,你可以看看parse_ini_file()函數。