Анализ файла данных с неограниченным количеством вложенных массивов с использованием BEGIN и END

Если вы чувствуете себя щедрым, ищите ответ в псевдо или работоспособном в PHP.

В настоящее время у меня есть файл со следующим синтаксисом:

ITEM 1
Pet Dog
Origin Earth
BEGIN Square
ITEM 2
BEGIN "0 0" Type "Earth over" Temp 0.1 END
BEGIN "0 1" Type "Earth over" Temp 0.1 END
BEGIN "0 2" Earth 0.1827 END
END
BEGIN "0100"ITEM 2
Origin Earth
BEGIN Alpha
ID 1
Name Alpha
END
BEGIN "Need"BEGIN "00" id.a END
BEGIN "01" id.b END
END
END

Я хотел бы иметь возможность взять это и разобрать это в пригодный для использования массив, подобный этому:

array(
"ITEM" => "1",
"Pet" => "Dog",
"Origin" => "Earth",
"Square" => array(
"ITEM" => "2",
"0 0" => array(
"Type" => "Earth over",
"Temp" => "0.1"),
"0 1" => array(
"Type" => "Earth over",
"Temp" => "0.1"),
"0 2" => array(
"Earth" => "0.1827"),
),
"0100" => array(
"ITEM" => "2",
"Origin" => "Earth",
"Alpha" => array(
"ID" => "1",
"Name" => "Alpha"),
"Need" => array(
"00" => "id.a",
"01" => "id.b")
)
)

Обратите внимание, что меня не волнуют типы значений — строки отлично подходят для того, что я делаю.

1

Решение

Это функция, которая анализирует этот текст. Это может потребовать корректировки и дополнения, если формат является более сложным, чем раскрыто в вопросе. Также требуется некоторая обработка ошибок, когда входные данные не соответствуют ожидаемому формату.

/**
* Parse the provided lines of text into an array.
* Warning: it modifies the input parameter. At the end of the execution, $lines will be empty.
*/
function parseData(array &$lines)
{
$result = array();                                 // this is where the current block is stored

// Process one line at a time...
do {
$l = trim(array_shift($lines));                // extract the first line into $l (and remove it from $lines)
$p = str_getcsv($l, ' ', '"');                 // split $l into words (take care of strings eclosed in quotes)

$key = array_shift($p);                        // get the first word from the line
switch ($key)
{
default:                                       // simple "key value" lines
$result[$key] = implode(' ', $p);          // combine the remaining words from $p into a single string
break;

case 'BEGIN':                                  // start a new block
$key = array_shift($p);                    // the real key in the array follows the 'BEGIN' keyword
if (end($p) == 'END') {                    // check if the entire block is on a single line
array_pop($p);                         // remove 'END' from the end of the array
if (count($p) == 1) {                  // only one word remaining in $p:
$result[$key] = array_shift($p);  //              it is the value
} else {                               // more than one word in $p: this is a list of properties
$aa = array();                    // put the values of this one-line block here
while (count($p) > 1) {
$k = array_shift($p);         // they come in pairs: key followed by value
$v = array_shift($p);
$aa[$k] = $v;
}
$result[$key] = $aa;              // the entire line was parsed; store it
}
} else {                                   // the last word on the line is not 'END'
$result[$key] = parseData($lines);     // recursively call this function to parse the inner block and store its result
}                                          // it will return after it consumes the line that starts with 'END'
break;

case 'END':                                    // here ends the block
return $result;                            // return the collected values
}

} while (count($lines));                           // ... until there are no more lines to process

return $result;                                    // no more lines to process; return the collected data
}

Как это использовать:

$text  = file_get_contents('1.txt');
$lines = explode("\n", trim($text));

$data = parseData($lines);
print_r($data);
0

Другие решения

Других решений пока нет …