3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Напишите реализацию для ф-ии strstr: * function strstr(string $haystack, string $needle) * нельзя использовать встроенные ф-ии php: substr, strtok, ... * (*) это более удачная реализация, работает за О(n) */ /** * Find substring starting in string, starting from some symbol(s) * @param string $haystack * @param string $needle * @return string */ function my_strstr2(string $haystack, string $needle): string { $isFound = false; $lenHaystack = strlen($haystack); $lenNeedle = strlen($needle); for($i = 0, $j = 0, $cnt = 0; $i < $lenHaystack, $j < $lenNeedle; $i++) { if($haystack[$i + $j] == $needle[$j]) { $j++; } else { $j = 0; } if($j >= strlen($lenNeedle)) { $isFound = true; break; } } return ($isFound ? substr($haystack, $i) : ''); } var_dump(my_strstr2('asdfghend', 'fg')); // 'fghend';
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XCanS
function name:  (null)
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'my_strstr2'
          2        SEND_VAL                                                 'asdfghend'
          3        SEND_VAL                                                 'fg'
          4        DO_FCALL                                      0  $0      
          5        SEND_VAR                                                 $0
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function my_strstr2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 11
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 36
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 11
Branch analysis from position: 29
Branch analysis from position: 11
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
Branch analysis from position: 24
filename:       /in/XCanS
function name:  my_strstr2
number of ops:  41
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $isFound, !3 = $lenHaystack, !4 = $lenNeedle, !5 = $i, !6 = $j, !7 = $cnt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        ASSIGN                                                   !2, <false>
   19     3        STRLEN                                           ~9      !0
          4        ASSIGN                                                   !3, ~9
   20     5        STRLEN                                           ~11     !1
          6        ASSIGN                                                   !4, ~11
   22     7        ASSIGN                                                   !5, 0
          8        ASSIGN                                                   !6, 0
          9        ASSIGN                                                   !7, 0
         10      > JMP                                                      ->25
   23    11    >   ADD                                              ~16     !5, !6
         12        FETCH_DIM_R                                      ~17     !0, ~16
         13        FETCH_DIM_R                                      ~18     !1, !6
         14        IS_EQUAL                                                 ~17, ~18
         15      > JMPZ                                                     ~19, ->18
   24    16    >   PRE_INC                                                  !6
   23    17      > JMP                                                      ->19
   26    18    >   ASSIGN                                                   !6, 0
   29    19    >   STRLEN                                           ~22     !4
         20        IS_SMALLER_OR_EQUAL                                      ~22, !6
         21      > JMPZ                                                     ~23, ->24
   30    22    >   ASSIGN                                                   !2, <true>
   31    23      > JMP                                                      ->29
   22    24    >   PRE_INC                                                  !5
         25    >   IS_SMALLER                                       ~26     !5, !3
         26        FREE                                                     ~26
         27        IS_SMALLER                                               !6, !4
         28      > JMPNZ                                                    ~27, ->11
   35    29    > > JMPZ                                                     !2, ->36
         30    >   INIT_FCALL                                               'substr'
         31        SEND_VAR                                                 !0
         32        SEND_VAR                                                 !5
         33        DO_ICALL                                         $28     
         34        QM_ASSIGN                                        ~29     $28
         35      > JMP                                                      ->37
         36    >   QM_ASSIGN                                        ~29     ''
         37    >   VERIFY_RETURN_TYPE                                       ~29
         38      > RETURN                                                   ~29
   36    39*       VERIFY_RETURN_TYPE                                       
         40*     > RETURN                                                   null

End of function my_strstr2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.79 ms | 1013 KiB | 16 Q