3v4l.org

run code in 300+ PHP versions simultaneously
<?php function find_words($in) { $len = strlen($in); if ($len < 2) { return $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(); $std_word = null; $upr_word = null; $num_word = null; $find_word = function () use (&$char, $alpha_num, $lower, $upper, $next, $peek, &$std_word, &$upr_word, &$num_word) { while (!$alpha_num($char)) { if (!$next()) { return null; } } if ($lower($char)) { return $std_word; // lowercase word } if ($upper($char)) { if ($upper($peek())) { // uppercase word return $upr_word; } return $std_word; // capitalized word } return $num_word; }; $upr_word = function() use (&$out, &$char, $next, $peek, $upper, $lower, $find_word) { $word = $char; while ($next() && $upper($char)) { if ($lower($peek())) { // start of capitalized word (e.g. FOOBar at B) break; } $word .= $char; } $out[] = $word; return $find_word; }; $std_word = function() use (&$out, &$char, $next, $lower, $find_word) { $word = $char; while ($next() && $lower($char)) { $word .= $char; } $out[] = $word; return $find_word; }; $num_word = function() use (&$out, &$char, $next, $num, $find_word) { $word = $char; while ($next() && $num($char)) { $word .= $char; } $out[] = $word; return $find_word; }; $state = $find_word; while ($state !== null) { $state = $state(); } return $out; } $samples = array( 'FooBar123', 'FooBAR123', 'FOOBar123', 'foo_bar_123', 'FOO_bar_123', 'FOO_Bar_123', 'foo_BAR_123', 'foo_Bar_123', 'Foo_bar_123', ); print_r(array_map(function ($name) { return array( 'name' => $name, 'words' => find_words($name), ); }, $samples));
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [name] => FooBar123 [words] => Array ( [0] => Foo [1] => Bar [2] => 123 ) ) [1] => Array ( [name] => FooBAR123 [words] => Array ( [0] => Foo [1] => BAR [2] => 123 ) ) [2] => Array ( [name] => FOOBar123 [words] => Array ( [0] => FOO [1] => Bar [2] => 123 ) ) [3] => Array ( [name] => foo_bar_123 [words] => Array ( [0] => foo [1] => bar [2] => 123 ) ) [4] => Array ( [name] => FOO_bar_123 [words] => Array ( [0] => FOO [1] => bar [2] => 123 ) ) [5] => Array ( [name] => FOO_Bar_123 [words] => Array ( [0] => FOO [1] => Bar [2] => 123 ) ) [6] => Array ( [name] => foo_BAR_123 [words] => Array ( [0] => foo [1] => BAR [2] => 123 ) ) [7] => Array ( [name] => foo_Bar_123 [words] => Array ( [0] => foo [1] => Bar [2] => 123 ) ) [8] => Array ( [name] => Foo_bar_123 [words] => Array ( [0] => Foo [1] => bar [2] => 123 ) ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
48.16 ms | 409 KiB | 8 Q