次のような.dictファイルからエントリを変換するにはどうすればよいですか:
aveu acknowledgement, admission
のようなphp配列に
$array['aveu'] = array( 1 => '承認', 2 => '入場');
助けてくれてありがとう!
次のような.dictファイルからエントリを変換するにはどうすればよいですか:
aveu acknowledgement, admission
のようなphp配列に
$array['aveu'] = array( 1 => '承認', 2 => '入場');
助けてくれてありがとう!
親の前に空白がなく、子レコードが空白で始まるカンマで区切られていると仮定すると、ファイル内の行をループします。( 経由でpreg_match()
) 先頭に空白がない場合は、新しい配列キーとexplode()
後続の空白行を開始します。
$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.";
}
}
}