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"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/kCLrm
function name:  (null)
number of ops:  23
compiled vars:  !0 = $array, !1 = $search, !2 = $functions, !3 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   11     1        ASSIGN                                                   !1, 'car'
   45     2        ASSIGN                                                   !2, <array>
   56     3      > FE_RESET_R                                       $7      !2, ->21
          4    > > FE_FETCH_R                                               $7, !3, ->21
   57     5    >   NOP                                                      
          6        FAST_CONCAT                                      ~8      !3, '%3A+'
          7        INIT_FCALL                                               'json_encode'
          8        INIT_FCALL                                               'array_values'
          9        INIT_DYNAMIC_CALL                                        !3
         10        SEND_VAR_EX                                              !0
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $9      
         13        SEND_VAR                                                 $9
         14        DO_ICALL                                         $10     
         15        SEND_VAR                                                 $10
         16        DO_ICALL                                         $11     
         17        CONCAT                                           ~12     ~8, $11
         18        CONCAT                                           ~13     ~12, '%0A'
         19        ECHO                                                     ~13
   56    20      > JMP                                                      ->4
         21    >   FE_FREE                                                  $7
   58    22      > RETURN                                                   1

Function keepifstartswith_regex:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  keepIfStartsWith_regex
number of ops:  16
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        INIT_FCALL                                               'preg_grep'
          3        INIT_FCALL                                               'preg_quote'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 '%2F'
          6        DO_ICALL                                         $2      
          7        CONCAT                                           ~3      '%2F%5E', $2
          8        CONCAT                                           ~4      ~3, '%2F'
          9        SEND_VAL                                                 ~4
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $5      
         12        VERIFY_RETURN_TYPE                                       $5
         13      > RETURN                                                   $5
   15    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function keepifstartswith_regex

Function removeifstartswith_regex:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  removeIfStartsWith_regex
number of ops:  17
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        INIT_FCALL                                               'preg_grep'
          3        INIT_FCALL                                               'preg_quote'
          4        SEND_VAR                                                 !1
          5        SEND_VAL                                                 '%2F'
          6        DO_ICALL                                         $2      
          7        CONCAT                                           ~3      '%2F%5E', $2
          8        CONCAT                                           ~4      ~3, '%2F'
          9        SEND_VAL                                                 ~4
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 1
         12        DO_ICALL                                         $5      
         13        VERIFY_RETURN_TYPE                                       $5
         14      > RETURN                                                   $5
   19    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function removeifstartswith_regex

Function keepifstartswith_php8:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  keepIfStartsWith_PHP8
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   22     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   23    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $v, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'str_starts_with'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6      > RETURN                                                   $2
          7*     > RETURN                                                   null

End of Dynamic Function 0

End of function keepifstartswith_php8

Function removeifstartswith_php8:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  removeIfStartsWith_PHP8
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   27    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $v, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'str_starts_with'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6        BOOL_NOT                                         ~3      $2
          7      > RETURN                                                   ~3
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of function removeifstartswith_php8

Function keepifstartswith_php7_4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  keepIfStartsWith_PHP7_4
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   30     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   31    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $v, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6        IS_IDENTICAL                                     ~3      $2, 0
          7      > RETURN                                                   ~3
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of function keepifstartswith_php7_4

Function removeifstartswith_php7_4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  removeIfStartsWith_PHP7_4
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   35    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $v, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6        IS_NOT_IDENTICAL                                 ~3      $2, 0
          7      > RETURN                                                   ~3
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of function removeifstartswith_php7_4

Function keepifstartswith_sub7_4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  keepIfStartsWith_sub7_4
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   38     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   39    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $v, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6        IS_IDENTICAL                                     ~3      $2, 0
          7      > RETURN                                                   ~3
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of function keepifstartswith_sub7_4

Function removeifstartswith_sub7_4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  removeIfStartsWith_sub7_4
number of ops:  12
compiled vars:  !0 = $haystack, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   42     2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
          7        DO_ICALL                                         $3      
          8        VERIFY_RETURN_TYPE                                       $3
          9      > RETURN                                                   $3
   43    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kCLrm
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $v, !1 = $needle
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6        IS_NOT_IDENTICAL                                 ~3      $2, 0
          7      > RETURN                                                   ~3
          8*     > RETURN                                                   null

End of Dynamic Function 0

End of function removeifstartswith_sub7_4

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.3 ms | 1095 KiB | 20 Q