<?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";
}
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.26, 8.4.1 - 8.4.13
- array (
'id' => '28',
'quantity' => '1',
'options' =>
array (
8 => '17',
7 => '15',
9 => '19',
),
)
---
array (
'id' => '28',
'quantity' => '1',
'options' =>
array (
8 => '17',
7 => '15',
),
)
---
array (
'id' => '28',
'quantity' => '1',
'options' =>
array (
8 => '17',
),
)
---
array (
'id' => '2',
'quantity' => NULL,
'options' =>
array (
),
)
---
invalid input
---
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array (
'id' => '28',
'quantity' => '1',
'options' =>
array (
8 => '17',
7 => '15',
9 => '19',
),
)
---
array (
'id' => '28',
'quantity' => '1',
'options' =>
array (
8 => '17',
7 => '15',
),
)
---
array (
'id' => '28',
'quantity' => '1',
'options' =>
array (
8 => '17',
),
)
---
array (
'id' => '2',
'quantity' => NULL,
'options' =>
array (
),
)
---
invalid input
---
preferences:
153.55 ms | 409 KiB | 5 Q