3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = 'My Interests:379{Commercial:0,Consumer:1,Wholesale Reseller:2},Test Group:1234{Test One:1,Test 2:2}'; $regex = '(?<group_name>[a-zA-Z0-9 _]+):(?<group_id>[0-9]+)(?={)|(?<=[{,])(?<option_name>[^:]+):(?<option_id>[^,}]+)(?=[,}])'; preg_match_all("/$regex/", $string, $matches); //print_r($matches); $groups = array_combine(array_filter($matches['group_name']), array_filter($matches['group_id'], function ($v) { return $v !== '';})); $options = array_combine(array_filter($matches['option_name']), array_filter($matches['option_id'], function ($v) { return $v !== '';})); print_r($groups); print_r($options); $output = array(); for ($i = 0; $i < count($matches['group_name']); $i++) { if ($matches['group_name'][$i] != '') { // new group $this_group = $matches['group_name'][$i]; $output[$this_group] = array('id' => $matches['group_id'][$i]); } else { // option for this group $output[$this_group]['options'][$matches['option_name'][$i]] = $matches['option_id'][$i]; } } print_r($output); $output = array(); $this_group = -1; for ($i = 0; $i < count($matches['group_name']); $i++) { if ($matches['group_name'][$i] != '') { // new group $this_group++; $output[$this_group] = array('name' => $matches['group_name'][$i], 'id' => $matches['group_id'][$i]); } else { // option for this group $output[$this_group]['options'][$matches['option_name'][$i]] = $matches['option_id'][$i]; } } print_r($output);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
Array ( [My Interests] => 379 [Test Group] => 1234 ) Array ( [Commercial] => 0 [Consumer] => 1 [Wholesale Reseller] => 2 [Test One] => 1 [Test 2] => 2 ) Array ( [My Interests] => Array ( [id] => 379 [options] => Array ( [Commercial] => 0 [Consumer] => 1 [Wholesale Reseller] => 2 ) ) [Test Group] => Array ( [id] => 1234 [options] => Array ( [Test One] => 1 [Test 2] => 2 ) ) ) Array ( [0] => Array ( [name] => My Interests [id] => 379 [options] => Array ( [Commercial] => 0 [Consumer] => 1 [Wholesale Reseller] => 2 ) ) [1] => Array ( [name] => Test Group [id] => 1234 [options] => Array ( [Test One] => 1 [Test 2] => 2 ) ) )
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 ( [My Interests] => 379 [Test Group] => 1234 ) Array ( [Commercial] => 0 [Consumer] => 1 [Wholesale Reseller] => 2 [Test One] => 1 [Test 2] => 2 ) Array ( [My Interests] => Array ( [id] => 379 [options] => Array ( [Commercial] => 0 [Consumer] => 1 [Wholesale Reseller] => 2 ) ) [Test Group] => Array ( [id] => 1234 [options] => Array ( [Test One] => 1 [Test 2] => 2 ) ) ) Array ( [0] => Array ( [name] => My Interests [id] => 379 [options] => Array ( [Commercial] => 0 [Consumer] => 1 [Wholesale Reseller] => 2 ) ) [1] => Array ( [name] => Test Group [id] => 1234 [options] => Array ( [Test One] => 1 [Test 2] => 2 ) ) )

preferences:
117.44 ms | 405 KiB | 214 Q