2012-07-26 76 views
-2

如何可以转换条目从.dict文件,如:转换.dict到阵列

aveu 
    acknowledgement, admission 

到PHP数组等

$阵列[ 'aveu'] = array(1 =>'acknowledgement',2 =>'admission');

感谢您的帮助!

回答

0

假设父代在它之前没有空格,并且子记录以空白开始以逗号分隔,则循环遍历文件中的行。如果前面没有空格(通过preg_match()),请启动一个新的数组键和后续的空白行。

$output = array(); 
$lines = file('yourfile.dict'); 
foreach ($lines as $line) { 
    // Skip blank lines 
    if (strlen(trim($line)) > 0) { 
    // No leading whitespace, start a new key: 
    if (!preg_match('/^\s+/', $line)) { 
     $key = trim($line); 
     $output[$key] = array(); 
    } 
    // Otherwise, explode and add to the previous $key (if $key is non-empty) 
    else if (!empty($key)) { 
     $terms = explode(",", $line); 
     // Trim off whitespace 
     $terms = array_map('trim', $terms); 
     // Merge them onto the existing key (if multiple lines) 
     $output[$key] = array_merge($output[$key], $terms); 
    } 
    else { 
     // Error - no current $key 
     echo "??? We don't have an active key."; 
    } 
    } 
}