3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Wrapper for strpbrk() except that it accepts a $length parameter * which will restrict the result string to this many characters. * * @param string $haystack The string where char_list is looked for. * @param string $char_list Case sensitive set of characters to look for. * @param int $length [optional] If given and is positive, the string returned will contain at most length characters. * @return false|string * @throws RangeException */ function strpbrkLength( string $haystack, string $char_list, int $length = null ) /*: false|string*/ { if( null !== $length && 0 > $length ) throw new RangeException( '$length must be null, zero or a positive integer.' ); $strpbrk = strpbrk( $haystack, $char_list ); if( false !== $strpbrk && null !== $length && strlen( $strpbrk ) > $length ) return substr( $strpbrk, 0, $length ); return $strpbrk; } $tests = [ [ 'haystack' => '', 'char_list' => 'r+', 'length' => null, 'expect' => false ], [ 'haystack' => '', 'char_list' => 'r+', 'length' => 0 , 'expect' => false ], [ 'haystack' => '', 'char_list' => 'r+', 'length' => 1 , 'expect' => false ], [ 'haystack' => 'r', 'char_list' => 'r+', 'length' => null, 'expect' => 'r' ], [ 'haystack' => 'r', 'char_list' => 'r+', 'length' => 0 , 'expect' => '' ], [ 'haystack' => 'r', 'char_list' => 'r+', 'length' => 1 , 'expect' => 'r' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => null, 'expect' => 'r+' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => 0 , 'expect' => '' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => 1 , 'expect' => 'r' ], [ 'haystack' => 'r+', 'char_list' => 'r+', 'length' => 2 , 'expect' => 'r+' ], [ 'haystack' => 'w', 'char_list' => 'r+', 'length' => null, 'expect' => false ], [ 'haystack' => 'w', 'char_list' => 'r+', 'length' => 0, 'expect' => false ], [ 'haystack' => 'w', 'char_list' => 'r+', 'length' => 1, 'expect' => false ], [ 'haystack' => 'w+', 'char_list' => 'r+', 'length' => null, 'expect' => '+' ], [ 'haystack' => 'w+', 'char_list' => 'r+', 'length' => 0 , 'expect' => '' ], [ 'haystack' => 'w+', 'char_list' => 'r+', 'length' => 1 , 'expect' => '+' ], ]; foreach( $tests as $test => $args ) assert( strpbrkLength( $args[ 'haystack' ], $args[ 'char_list' ], $args[ 'length' ] ) === $args[ 'expect' ], "Failed test #$test" );
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 22
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 22
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/cp19M
function name:  (null)
number of ops:  24
compiled vars:  !0 = $tests, !1 = $args, !2 = $test
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  28     0  E >   QM_ASSIGN                                                !0
  50     1      > FE_RESET_R                                       $4      !0, ->22
         2    > > FE_FETCH_R                                       ~5      $4, !1, ->22
         3    >   QM_ASSIGN                                                !2
  51     4        ASSERT_CHECK                                             
         5        INIT_FCALL                                               'assert'
  52     6        INIT_FCALL                                               'strpbrklength'
         7        FETCH_DIM_R                                      ~7      !1, 'haystack'
         8        SEND_VAL                                                 ~7
         9        FETCH_DIM_R                                      ~8      !1, 'char_list'
        10        SEND_VAL                                                 ~8
        11        FETCH_DIM_R                                      ~9      !1, 'length'
        12        SEND_VAL                                                 ~9
        13        DO_FCALL                                      0  $10     
        14        FETCH_DIM_R                                      ~11     !1, 'expect'
        15        IS_NOT_IDENTICAL                                 ~12     $10, ~11
        16        SEND_VAL                                                 ~12
  53    17        NOP                                                      
        18        FAST_CONCAT                                      ~13     'Failed+test+%23', !2
        19        SEND_VAL                                                 ~13
        20        DO_ICALL                                                 
  50    21      > JMP                                                      ->2
        22    >   FE_FREE                                                  $4
  54    23      > RETURN                                                   1

Function strpbrklength:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 32
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 21
Branch analysis from position: 7
filename:       /in/cp19M
function name:  strpbrkLength
number of ops:  34
compiled vars:  !0 = $haystack, !1 = $char_list, !2 = $length, !3 = $strpbrk
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  13     0  E >   RECV                                             !0      
         1        RECV                                             !1      
         2        RECV_INIT                                        !2      null
  16     3        IS_EQUAL                                         ~4      !2, null
         4      > JMPZ_EX                                          ~4      ~4, ->7
         5    >   IS_SMALLER_OR_EQUAL                              ~5      !2, 0
         6        BOOL                                             ~4      ~5
         7    > > JMPZ                                                     ~4, ->12
  17     8    >   NEW                                              $6      :50
         9        SEND_VAL_EX                                              '%24length+must+be+null%2C+zero+or+a+positive+integer.'
        10        DO_FCALL                                      0          
        11      > THROW                                         0          $6
  19    12    >   INIT_FCALL                                               'strpbrk'
        13        SEND_VAR                                                 !0
        14        SEND_VAR                                                 !1
        15        DO_ICALL                                         $8      
        16        QM_ASSIGN                                                !3
  21    17        IS_EQUAL                                         ~10     !3, <false>
        18      > JMPZ_EX                                          ~10     ~10, ->21
        19    >   IS_EQUAL                                         ~11     !2, null
        20        BOOL                                             ~10     ~11
        21    > > JMPZ_EX                                          ~10     ~10, ->25
        22    >   STRLEN                                           ~12     !3
        23        IS_SMALLER_OR_EQUAL                              ~13     !2, ~12
        24        BOOL                                             ~10     ~13
        25    > > JMPZ                                                     ~10, ->32
  22    26    >   INIT_FCALL                                               'substr'
        27        SEND_VAR                                                 !3
        28        SEND_VAL                                                 0
        29        SEND_VAR                                                 !2
        30        DO_ICALL                                         $14     
        31      > RETURN                                                   $14
  24    32    > > RETURN                                                   !3
  25    33*     > RETURN                                                   null

End of function strpbrklength

Generated using Vulcan Logic Dumper, using php 7.4.0