Поскольку значения могут содержать тире и распределяться по нескольким строкам, я думаю, что самый безопасный способ отделения ключей от значений - это использование substr()
, поскольку разделяющие тире всегда находятся в одной и той же позиции символа в строке.
FIXED
<?php
// first, split into lines
$lines = explode("\n",str_replace(array("\r\n","\r"),"\n",$data));
// this will hold the parsed data
$result = array();
// This will track the current key for multi-line values
$thisKey = '';
// Loop the split data
foreach ($lines as $line) {
if (substr($line,4,1) == '-') {
// There is a separator, start a new key
$thisKey = trim(substr($line,0,4));
if ($result[$thisKey]) {
// This is a duplicate value
if (is_array($result[$thisKey])) {
// already an array
$result[$thisKey][] = trim(substr($line,5));
} else {
// convert to array
$result[$thisKey] = array($result[$thisKey],trim(substr($line,5)));
}
} else {
// Not a duplicate value
$result[$thisKey] = trim(substr($line,5));
}
} else {
// There is no separator, append data to the last key
if (is_array($result[$thisKey])) {
$result[$thisKey][count($result[$thisKey]) - 1] .= PHP_EOL.trim(substr($line,5));
} else {
$result[$thisKey] .= PHP_EOL.trim(substr($line,5));
}
}
}
print_r($result);
?>
Посмотри, как работает