<?php
$tests = [
'28{8}17{7}15{9}19[1]',
'28{8}17{7}15[1]',
'28{8}17[1]',
'28[1]',
'28{8}17[1',
];
foreach($tests as $test) {
if (!preg_match_all('~^\d+(?=.+\[\d+]$)|\G(?!^)(?:{\K\d+}\d+|\[\K\d(?=]$))~', $test, $m)) {
echo "invalid input";
} else {
var_export(
[
'id' => array_shift($m[0]),
'quantity' => array_pop($m[0]),
'options' => array_reduce(
$m[0],
function($result, $string) {
[$key, $result[$key]] = explode('}', $string, 2);
return $result;
},
[]
)
]
);
}
echo "\n---\n";
}
preferences:
25.28 ms | 409 KiB | 5 Q