Вопрос
В Linux.
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-controffields.html
Если я допустил ошибки, исправлены исправления!
Не связан с StackOverflow