0

Linux では、.DEB ファイルには、次のように配置された「コントロール」テキスト ファイルがあります。

Name: Value
Size: Value
Information: Mutliline
             value

制御ファイルを次のような PHP 配列にする最良の方法は何ですか?

Array ( "Name" => Value, "Size" => Value, "Information" => Value);

値は複数行にすることができ、「:」セパレーターを含めることができることに注意してください。

ありがとう!

4

2 に答える 2

1
$source = fopen('path/to/file');
$index = '';
while( ($line = fgets($source)) !== false ){
    if(preg_match('/^\s*$/', $line))
        continue 1; // ignore empty lines //

    if(!preg_match('/^\s+/', $line)){ // if the line does not start with whitespace then it has a new key-value pair //
        $items = explode(':', $line, 2); // separate at the first : //
        $index = strtolower($items[0]); // the keys are case insensitive //
        $value = preg_replace('/^\s+/', '', $items[1]); // remove extra whitespace from the begining //
        $value = preg_replace('/\s+$/', '', $value); // and from the end //
    }
    else{ // continue the value from the previous line //
        $value = preg_replace('/\s+$/', '', $line); // remove whitespace only from the end //
    }
    $data[$index] .= $value;
}
fclose($source);

ここで説明されているように実装されています: http://www.debian.org/doc/debian-policy/ch-controlfields.html

間違いがあれば修正を歓迎します!

于 2010-12-08T22:19:19.967 に答える