fork download
  1. <?php
  2. $code='array(0 => "a", 123 => 123, $_POST["hello"][\'world\'] => array("is", "actually", "An array !"), 1234, \'got problem ?\',
  3. "a" => $GlobalScopeVar, $test_further => function test($noway){echo "this works too !!!";}, "yellow" => "blue",
  4. "b" => array("nested"=>array(1,2,3), "nested"=>array(1,2,3),"nested"=>array(1,2,3)), "c" => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }
  5. "bug", "fixed", "mwahahahaa" => "Yeaaaah"
  6. );'; // Sample data
  7.  
  8. $code = preg_replace('#(^\s*array\s*\(\s*)|(\s*\)\s*;?\s*$)#s', '', $code); // Just to get ride of array( at the beginning, and ); at the end
  9.  
  10. \s* # white spaces
  11. ########################## KEYS START ##########################
  12. (?: # We\'ll use this to make keys optional
  13. (?P<keys> # named group: keys
  14. \d+ # match digits
  15. | # or
  16. "(?(?=\\\\")..|[^"])*" # match string between "", works even 4 escaped ones "hello \" world"
  17. | # or
  18. \'(?(?=\\\\\')..|[^\'])*\' # match string between \'\', same as above :p
  19. | # or
  20. \$\w+(?:\[(?:[^[\]]|(?R))*\])* # match variables $_POST, $var, $var["foo"], $var["foo"]["bar"], $foo[$bar["fail"]]
  21. ) # close group: keys
  22. ########################## KEYS END ##########################
  23. \s* # white spaces
  24. => # match =>
  25. )? # make keys optional
  26. \s* # white spaces
  27. ########################## VALUES START ##########################
  28. (?P<values> # named group: values
  29. \d+ # match digits
  30. | # or
  31. "(?(?=\\\\")..|[^"])*" # match string between "", works even 4 escaped ones "hello \" world"
  32. | # or
  33. \'(?(?=\\\\\')..|[^\'])*\' # match string between \'\', same as above :p
  34. | # or
  35. \$\w+(?:\[(?:[^[\]]|(?R))*\])* # match variables $_POST, $var, $var["foo"], $var["foo"]["bar"], $foo[$bar["fail"]]
  36. | # or
  37. array\s*\((?:[^()]|(?R))*\) # match an array()
  38. | # or
  39. \[(?:[^[\]]|(?R))*\] # match an array, new PHP array syntax: [1, 3, 5] is the same as array(1,3,5)
  40. | # or
  41. (?:function\s+)?\w+\s* # match functions: helloWorld, function name
  42. (?:\((?:[^()]|(?R))*\)) # match function parameters (wut), (), (array(1,2,4))
  43. (?:(?:\s*use\s*\((?:[^()]|(?R))*\)\s*)? # match use(&$var), use($foo, $bar) (optionally)
  44. \{(?:[^{}]|(?R))*\} # match { whatever}
  45. )?;? # match ; (optionally)
  46. ) # close group: values
  47. ########################## VALUES END ##########################
  48. \s* # white spaces
  49. ~xsi', $code, $m); // Matching :p
  50.  
  51. print_r($m['keys']); // Print keys
  52. print_r($m['values']); // Print values
  53.  
  54.  
  55. // Since some keys may be empty in case you didn't specify them in the array, let's fill them up !
  56. foreach($m['keys'] as $index => &$key){
  57. if($key === ''){
  58. $key = 'made_up_index_'.$index;
  59. }
  60. }
  61. $results = array_combine($m['keys'], $m['values']);
  62. print_r($results); // printing results
  63. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 0
    [1] => 123
    [2] => $_POST["hello"]['world']
    [3] => 
    [4] => 
    [5] => "a"
    [6] => $test_further
    [7] => "yellow"
    [8] => "b"
    [9] => "c"
    [10] => 
    [11] => 
    [12] => "mwahahahaa"
)
Array
(
    [0] => "a"
    [1] => 123
    [2] => array("is", "actually", "An array !")
    [3] => 1234
    [4] => 'got problem ?'
    [5] => $GlobalScopeVar
    [6] => function test($noway){echo "this works too !!!";}
    [7] => "blue"
    [8] => array("nested"=>array(1,2,3), "nested"=>array(1,2,3),"nested"=>array(1,2,3))
    [9] => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }
    [10] => "bug"
    [11] => "fixed"
    [12] => "Yeaaaah"
)
Array
(
    [0] => "a"
    [123] => 123
    [$_POST["hello"]['world']] => array("is", "actually", "An array !")
    [made_up_index_3] => 1234
    [made_up_index_4] => 'got problem ?'
    ["a"] => $GlobalScopeVar
    [$test_further] => function test($noway){echo "this works too !!!";}
    ["yellow"] => "blue"
    ["b"] => array("nested"=>array(1,2,3), "nested"=>array(1,2,3),"nested"=>array(1,2,3))
    ["c"] => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }
    [made_up_index_10] => "bug"
    [made_up_index_11] => "fixed"
    ["mwahahahaa"] => "Yeaaaah"
)