3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'car_porsche', 'caravan', 'car', 'motorcycle_suzuki', 'motorcycle_carabela', ]; $search = 'car'; function keepIfStartsWith_regex(array $haystack, string $needle): array { return preg_grep('/^' . preg_quote($needle, '/') . '/', $haystack); } function removeIfStartsWith_regex(array $haystack, string $needle): array { return preg_grep('/^' . preg_quote($needle, '/') . '/', $haystack, PREG_GREP_INVERT); } function keepIfStartsWith_PHP8(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => str_starts_with($v, $needle)); } function removeIfStartsWith_PHP8(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => !str_starts_with($v, $needle)); } function keepIfStartsWith_PHP7_4(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => strpos($v, $needle) === 0); } function removeIfStartsWith_PHP7_4(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => strpos($v, $needle) !== 0); } function keepIfStartsWith_sub7_4(array $haystack, string $needle): array { return array_filter($haystack, function($v) use($needle) { return strpos($v, $needle) === 0; }); } function removeIfStartsWith_sub7_4(array $haystack, string $needle): array { return array_filter($haystack, function($v) use($needle) { return strpos($v, $needle) !== 0; }); } $functions = [ 'keepIfStartsWith_regex', 'removeIfStartsWith_regex', 'keepIfStartsWith_PHP8', 'removeIfStartsWith_PHP8', 'keepIfStartsWith_PHP7_4', 'removeIfStartsWith_PHP7_4', 'keepIfStartsWith_sub7_4', 'removeIfStartsWith_sub7_4', ]; foreach ($functions as $fn) { echo "$fn: " . json_encode(array_values($fn($array, $search))) . "\n"; }
Output for git.master, git.master_jit, rfc.property-hooks
keepIfStartsWith_regex: ["car_porsche","caravan","car"] removeIfStartsWith_regex: ["motorcycle_suzuki","motorcycle_carabela"] keepIfStartsWith_PHP8: ["car_porsche","caravan","car"] removeIfStartsWith_PHP8: ["motorcycle_suzuki","motorcycle_carabela"] keepIfStartsWith_PHP7_4: ["car_porsche","caravan","car"] removeIfStartsWith_PHP7_4: ["motorcycle_suzuki","motorcycle_carabela"] keepIfStartsWith_sub7_4: ["car_porsche","caravan","car"] removeIfStartsWith_sub7_4: ["motorcycle_suzuki","motorcycle_carabela"]

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:
24.9 ms | 407 KiB | 5 Q