Skip to content

Instantly share code, notes, and snippets.

@Philipp91
Created February 25, 2018 18:31
Show Gist options
  • Save Philipp91/2acbff5835a0365b84dc26079d2d935e to your computer and use it in GitHub Desktop.
Save Philipp91/2acbff5835a0365b84dc26079d2d935e to your computer and use it in GitHub Desktop.
<?php
function parseDescription($descr)
{
$prepared = array();
$result = array();
// prefill with empty values
for ($i = 0; $i <= 63; $i++) {
$prepared[$i] = null;
}
$descr = str_replace("\r\n", '', $descr);
$descr = str_replace('? ', '?', $descr);
preg_match_all('/\?[\r\n]*(\d{2})([^\?]+)/', $descr, $matches, PREG_SET_ORDER);
$descriptionLines = array();
$description1 = ''; // Legacy, could be removed.
$description2 = ''; // Legacy, could be removed.
foreach ($matches as $m) {
$index = (int) $m[1];
if ((20 <= $index && $index <= 29) || (60 <= $index && $index <= 63)) {
if (20 <= $index && $index <= 29) {
$description1 .= $m[2];
} else {
$description2 .= $m[2];
}
$m[2] = trim($m[2]);
if (!empty($m[2])) {
$descriptionLines[] = $m[2];
}
} else {
$prepared[$index] = $m[2];
}
}
print_r($descriptionLines);
$description = array();
if (empty($descriptionLines) || strlen($descriptionLines[0]) < 5 || $descriptionLines[0][4] !== '+') {
$description['SVWZ'] = implode('', $descriptionLines);
} else {
$lastType = null;
foreach ($descriptionLines as $line) {
if (strlen($line) > 5 && $line[4] === '+') {
if ($lastType != null) {
$description[$lastType] = trim($description[$lastType]);
}
$lastType = substr($line, 0, 4);
$description[$lastType] = substr($line, 5);
} else {
$description[$lastType] .= $line;
}
if (strlen($line) < 27) {
// Usually, lines are 27 characters long. In case characters are missing, then it's either the end
// of the current type or spaces have been trimmed from the end. We want to collapse multiple spaces
// into one and we don't want to leave trailing spaces behind. So add a single space here to make up
// for possibly missing spaces, and if it's the end of the type, it will be trimmed off later.
$description[$lastType] .= ' ';
}
}
$description[$lastType] = trim($description[$lastType]);
}
$result['description'] = $description;
$result['booking_text'] = trim($prepared[0]);
$result['primanoten_nr'] = trim($prepared[10]);
$result['description_1'] = trim($description1);
$result['bank_code'] = trim($prepared[30]);
$result['account_number'] = trim($prepared[31]);
$result['name'] = trim($prepared[32] . $prepared[33]);
$result['text_key_addition'] = trim($prepared[34]);
$result['description_2'] = trim($description2);
return $result;
}
print_r(parseDescription("166?00GUTSCHRIFT?109251?20SVWZ+Rechnung xxx?30DEUTDEDB276?3\r\n1DE1127xxxxx40011xxxxxx?32Testfirma GmbH"));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment