3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tests = <<<EOL 1+2i+3j+4k => [1 2 3 4] -1+3i-3j+7k => [-1 3 -3 7] -1-4i-9j-2k => [-1 -4 -9 -2] 17-16i-15j-14k => [17 -16 -15 -14] 7+2i => [7 2 0 0] 2i-6k => [0 2 0 -6] 1-5j+2k => [1 0 -5 2] 3+4i-9k => [3 4 0 -9] 42i+j-k => [0 42 1 -1] 6-2i+j-3k => [6 -2 1 -3] 1+i+j+k => [1 1 1 1] -1-i-j-k => [-1 -1 -1 -1] 16k-20j+2i-7 => [-7 2 -20 16] i+4k-3j+2 => [2 1 -3 4] 5k-2i+9+3j => [9 -2 3 5] 5k-2j+3 => [3 0 -2 5] 1.75-1.75i-1.75j-1.75k => [1.75 -1.75 -1.75 -1.75] 2.0j-3k+0.47i-13 => [-13 0.47 2.0 -3] 5.6-3i => [5.6 -3 0 0] k-7.6i => [0 -7.6 0 1] 0 => [0 0 0 0] 0j+0k => [0 0 0 0] -0j => [0 0 0 0] 1-0k => [1 0 0 0] EOL; $lines = explode("\n", $tests); foreach($lines as $line) { list($test, $result) = explode('=>', $line); $test = trim($test); $result = preg_replace("/\s+/", ",", trim($result)); // Fake stdin $argv[1] = $test; // Golfed solution $a=[''=>0,'i'=> 0,'j'=>0,'k'=>0]; preg_match_all("/([-+]?)(\d*(\.\d+)?)([ijk]?)/",$argv[1],$m,2); foreach($m as$n)if($n[0])$a[$n[4]]=$n[1].($n[2]===''?1:$n[2]); echo "Test: " . $test, "\n"; echo "Expect: " . $result, "\n"; echo "Actual: [" . implode(',', $a), "]\n"; echo "\n"; }
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.28, 7.4.0 - 7.4.16, 8.0.0 - 8.0.3
Test: 1+2i+3j+4k Expect: [1,2,3,4] Actual: [1,+2,+3,+4] Test: -1+3i-3j+7k Expect: [-1,3,-3,7] Actual: [-1,+3,-3,+7] Test: -1-4i-9j-2k Expect: [-1,-4,-9,-2] Actual: [-1,-4,-9,-2] Test: 17-16i-15j-14k Expect: [17,-16,-15,-14] Actual: [17,-16,-15,-14] Test: 7+2i Expect: [7,2,0,0] Actual: [7,+2,0,0] Test: 2i-6k Expect: [0,2,0,-6] Actual: [0,2,0,-6] Test: 1-5j+2k Expect: [1,0,-5,2] Actual: [1,0,-5,+2] Test: 3+4i-9k Expect: [3,4,0,-9] Actual: [3,+4,0,-9] Test: 42i+j-k Expect: [0,42,1,-1] Actual: [0,42,+1,-1] Test: 6-2i+j-3k Expect: [6,-2,1,-3] Actual: [6,-2,+1,-3] Test: 1+i+j+k Expect: [1,1,1,1] Actual: [1,+1,+1,+1] Test: -1-i-j-k Expect: [-1,-1,-1,-1] Actual: [-1,-1,-1,-1] Test: 16k-20j+2i-7 Expect: [-7,2,-20,16] Actual: [-7,+2,-20,16] Test: i+4k-3j+2 Expect: [2,1,-3,4] Actual: [+2,1,-3,+4] Test: 5k-2i+9+3j Expect: [9,-2,3,5] Actual: [+9,-2,+3,5] Test: 5k-2j+3 Expect: [3,0,-2,5] Actual: [+3,0,-2,5] Test: 1.75-1.75i-1.75j-1.75k Expect: [1.75,-1.75,-1.75,-1.75] Actual: [1.75,-1.75,-1.75,-1.75] Test: 2.0j-3k+0.47i-13 Expect: [-13,0.47,2.0,-3] Actual: [-13,+0.47,2.0,-3] Test: 5.6-3i Expect: [5.6,-3,0,0] Actual: [5.6,-3,0,0] Test: k-7.6i Expect: [0,-7.6,0,1] Actual: [0,-7.6,0,1] Test: 0 Expect: [0,0,0,0] Actual: [0,0,0,0] Test: 0j+0k Expect: [0,0,0,0] Actual: [0,0,0,+0] Test: -0j Expect: [0,0,0,0] Actual: [0,0,-0,0] Test: 1-0k Expect: [1,0,0,0] Actual: [1,0,0,-0]