3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function do_split($name) { return preg_split('/([A-Z]{2,}|([A-Z][^A-Z]*))/', $name, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); } function convert_case($name) { return implode( '_', array_map( 'strtolower', do_split($name) ) ); } function convert_case2($in) { $len = strlen($in); if ($len < 2) { return strtolower($in); } // no state $upper = function ($c) { return 'A' <= $c && $c <= 'Z'; }; $lower = function ($c) { return 'a' <= $c && $c <= 'z'; }; $alpha = function ($c) use ($upper, $lower) { return $upper($c) || $lower($c); }; $num = function ($c) { return '0' <= $c && $c <= '9'; }; $alpha_num = function ($c) use ($alpha, $num) { return $alpha($c) || $num($c); }; // stream state $pos = 0; $char = $in[0]; $peek = function() use ($in, &$pos, $len) { if ($pos+1 >= $len) { return null; } return $in[$pos+1]; }; $next = function() use ($peek, &$pos, &$char) { $char = $peek(); $pos++; return $char !== null; }; // lex state $out = array(); $find_word = null; $upper_word = null; $lower_word = null; $cap_word = null; $num_word = null; $find_word = function () use (&$char, $alpha_num, $lower, $upper, $next, $peek, &$lower_word, &$upper_word, &$cap_word, &$num_word) { while (!$alpha_num($char)) { if (!$next()) { return null; } } if ($lower($char)) { return $lower_word; } if ($upper($char)) { if ($upper($peek())) { // uppercase word return $upper_word; } return $cap_word; // capitalized word } return $num_word; }; $upper_word = function() use (&$out, &$char, $next, $upper, &$find_word) { $word = $char; while ($next() && $upper($char)) { $word .= $char; } $out[] = $word; return $find_word; }; $state = $find_word; while ($state !== null) { $state = $state(); } return strtolower(implode('_', $out)); } convert_case2('FooBARbar0123'); $samples = array( 'FooBar', 'FooBAR', 'FOOBar', 'foo_bar', 'FOO_bar', 'FOO_Bar', 'foo_BAR', 'foo_Bar', 'Foo_bar', ); print_r(array_map(function ($name) { return array( 'name' => $name, 'converted' => convert_case2($name), // 'converted' => convert_case($name), ); }, $samples));
based on CWhCc

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.1.100.0000.01118.30
7.1.70.0030.00617.08
7.1.60.0160.00919.25
7.1.50.0070.01416.94
7.1.00.0100.07022.40
7.0.200.0000.00716.75
7.0.140.0070.07322.08
7.0.120.0000.07322.10
7.0.110.0030.07722.07
7.0.100.0030.05722.08
7.0.90.0030.07722.12
7.0.80.0030.07322.04
7.0.70.0130.06022.14
7.0.60.0070.07022.12
7.0.50.0070.07022.13
7.0.40.0030.06022.09
7.0.30.0030.07322.12
7.0.20.0100.06721.96
7.0.10.0000.07322.18
7.0.00.0000.06022.20
5.6.280.0000.07721.18
5.6.260.0070.06320.84
5.6.250.0000.07721.05
5.6.240.0000.08321.14
5.6.230.0030.07720.98
5.6.220.0030.07321.13
5.6.210.0070.06721.07
5.6.200.0000.08020.97
5.6.190.0030.06721.13
5.6.180.0000.07720.88
5.6.170.0070.05721.08
5.6.160.0070.07020.98
5.6.150.0030.07021.09
5.6.140.0070.07320.84
5.6.130.0070.05021.05
5.6.120.0070.07021.08
5.6.110.0030.07721.06
5.6.100.0030.07320.88
5.6.90.0070.07020.78
5.6.80.0100.05320.36
5.6.70.0100.06320.35
5.6.60.0030.07020.50
5.6.50.0030.06720.36
5.6.40.0030.05320.49
5.6.30.0070.06720.21
5.6.20.0070.07020.25
5.6.10.0000.07320.38
5.6.00.0070.06720.13
5.5.380.0000.07017.59
5.5.370.0000.07017.60
5.5.360.0070.07017.67
5.5.350.0000.07017.50
5.5.340.0000.07717.95
5.5.330.0070.05317.87
5.5.320.0030.07017.97
5.5.310.0100.06318.23
5.5.300.0070.06718.05
5.5.290.0000.07318.25
5.5.280.0000.05317.99
5.5.270.0100.07018.22
5.5.260.0070.07317.93
5.5.250.0070.07017.68
5.5.240.0070.06017.25
5.5.230.0070.06717.54
5.5.220.0000.07317.61
5.5.210.0000.06717.60
5.5.200.0100.04717.34
5.5.190.0030.06017.32
5.5.180.0030.07317.45
5.5.160.0030.05717.30
5.5.150.0030.05317.51
5.5.140.0030.06717.31
5.5.130.0070.06317.29
5.5.120.0100.06317.57
5.5.110.0070.06317.29
5.5.100.0030.07317.48
5.5.90.0000.07317.11
5.5.80.0000.05317.47
5.5.70.0030.03317.21
5.5.60.0000.06017.46
5.5.50.0000.07017.34
5.5.40.0130.06017.34
5.5.30.0030.05017.36
5.5.20.0000.07717.29
5.5.10.0030.07017.34
5.5.00.0000.05317.33
5.4.450.0030.06719.57
5.4.440.0030.07319.27
5.4.430.0000.07319.48
5.4.420.0070.06019.63
5.4.410.0030.06319.18
5.4.400.0030.06319.34
5.4.390.0000.07019.34
5.4.380.0030.06719.19
5.4.370.0070.05319.31
5.4.360.0030.06719.01
5.4.350.0070.06319.25
5.4.340.0000.07719.01
5.4.320.0100.06319.31
5.4.310.0070.05019.00
5.4.300.0030.06719.33
5.4.290.0000.07318.98
5.4.280.0000.07319.16
5.4.270.0000.07019.32
5.4.260.0070.06018.99
5.4.250.0100.06319.24
5.4.240.0000.03719.25
5.4.230.0030.06719.08
5.4.220.0000.07719.30
5.4.210.0000.07018.93
5.4.200.0070.05719.02
5.4.190.0070.05319.00
5.4.180.0000.06019.32
5.4.170.0030.04719.13
5.4.160.0070.06318.99
5.4.150.0030.07319.01
5.4.140.0030.06316.39
5.4.130.0070.06316.60
5.4.120.0030.06316.43
5.4.110.0030.06716.60
5.4.100.0100.05016.66
5.4.90.0000.06716.59
5.4.80.0070.05716.53
5.4.70.0070.05316.54
5.4.60.0070.06316.58
5.4.50.0070.06016.52
5.4.40.0000.06716.34
5.4.30.0000.06316.42
5.4.20.0070.06016.38
5.4.10.0000.04016.38
5.4.00.0030.05315.98
5.3.290.0000.07714.82
5.3.280.0000.07014.86
5.3.270.0030.06314.82
5.3.260.0030.05714.77
5.3.250.0000.07014.87
5.3.240.0000.07014.73
5.3.230.0030.06714.69
5.3.220.0070.06314.73
5.3.210.0030.07014.56
5.3.200.0100.05314.55
5.3.190.0000.06714.93
5.3.180.0000.06314.66
5.3.170.0030.05714.71
5.3.160.0030.06314.79
5.3.150.0030.06714.65
5.3.140.0030.06714.92
5.3.130.0030.06714.72
5.3.120.0030.05314.91
5.3.110.0030.04314.76
5.3.100.0070.05014.20
5.3.90.0130.05714.15
5.3.80.0030.05714.14
5.3.70.0000.06714.23
5.3.60.0030.06714.13
5.3.50.0000.05714.17
5.3.40.0030.06314.07
5.3.30.0030.05014.02
5.3.20.0070.05013.84
5.3.10.0000.05713.92
5.3.00.0030.05013.78
5.2.170.0000.06011.28
5.2.160.0070.04311.14
5.2.150.0030.05311.14
5.2.140.0030.05711.29
5.2.130.0030.05311.17
5.2.120.0000.05711.19
5.2.110.0000.05711.12
5.2.100.0030.05311.23
5.2.90.0000.05311.17
5.2.80.0030.04311.23
5.2.70.0030.05311.16
5.2.60.0030.05011.14
5.2.50.0070.04711.03
5.2.40.0030.04710.96
5.2.30.0030.04010.83
5.2.20.0000.04710.96
5.2.10.0070.05010.86
5.2.00.0000.05310.80
5.1.60.0000.05010.28
5.1.50.0000.04010.28
5.1.40.0030.04310.28
5.1.30.0000.05010.35
5.1.20.0000.05310.39
5.1.10.0100.04010.33
5.1.00.0070.03710.28
5.0.50.0030.04010.28
5.0.40.0030.03710.28
5.0.30.0000.06010.28
5.0.20.0030.03710.28
5.0.10.0000.03710.28
5.0.00.0030.05310.28
4.4.90.0000.02710.28
4.4.80.0000.03010.28
4.4.70.0000.03010.28
4.4.60.0000.03010.28
4.4.50.0000.02710.28
4.4.40.0000.03010.28
4.4.30.0030.01310.28
4.4.20.0000.03010.28
4.4.10.0000.03010.28
4.4.00.0000.04310.28
4.3.110.0000.03010.28
4.3.100.0000.03010.28
4.3.90.0000.03010.28
4.3.80.0000.04710.28
4.3.70.0000.03010.28
4.3.60.0000.03010.28
4.3.50.0000.02710.28
4.3.40.0000.04710.28
4.3.30.0030.02310.28
4.3.20.0000.02710.28
4.3.10.0000.02710.28
4.3.00.0000.02010.28