質問

Linuxでは、.debファイルには、次のように配置された「制御」テキストファイルがあります。

Name: Value
Size: Value
Information: Mutliline
             value

コントロールファイルをPHPアレイに似たものにするための最良の方法は何ですか:

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

値はマルチラインであり、「:」セパレーターを含めることができることに留意してください。

ありがとう!

役に立ちましたか?

解決

$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

私が間違いを犯した場合、修正を歓迎します!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top