3v4l.org

run code in 300+ PHP versions simultaneously
<?php function firstNWords(string $string, int $limit = 3) { return preg_replace("/(?:\W*\w+){{$limit}}\K.*/", '', $string); } var_dump(firstNWords("hello yes, world wah ha ha")); # => "hello yes, world" var_dump(firstNWords("hello yes,world wah ha ha")); # => "hello yes,world" var_dump(firstNWords("hello yes world wah ha ha")); # => "hello yes world" var_dump(firstNWords("hello yes world")); # => "hello yes world" var_dump(firstNWords("hello yes world.")); # => "hello yes world" var_dump(firstNWords("hello yes")); # => "hello yes" var_dump(firstNWords("hello")); # => "hello" var_dump(firstNWords("a")); # => "a" var_dump(firstNWords("")); # => ""
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ocRD7
function name:  (null)
number of ops:  55
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'firstnwords'
          2        SEND_VAL                                                 'hello+yes%2C+world+wah+ha+ha'
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
    8     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'firstnwords'
          8        SEND_VAL                                                 'hello+yes%2Cworld+wah+ha+ha'
          9        DO_FCALL                                      0  $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                                 
    9    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL                                               'firstnwords'
         14        SEND_VAL                                                 'hello+yes+world+wah+ha+ha'
         15        DO_FCALL                                      0  $4      
         16        SEND_VAR                                                 $4
         17        DO_ICALL                                                 
   10    18        INIT_FCALL                                               'var_dump'
         19        INIT_FCALL                                               'firstnwords'
         20        SEND_VAL                                                 'hello+yes+world'
         21        DO_FCALL                                      0  $6      
         22        SEND_VAR                                                 $6
         23        DO_ICALL                                                 
   11    24        INIT_FCALL                                               'var_dump'
         25        INIT_FCALL                                               'firstnwords'
         26        SEND_VAL                                                 'hello+yes+world.'
         27        DO_FCALL                                      0  $8      
         28        SEND_VAR                                                 $8
         29        DO_ICALL                                                 
   12    30        INIT_FCALL                                               'var_dump'
         31        INIT_FCALL                                               'firstnwords'
         32        SEND_VAL                                                 'hello+yes'
         33        DO_FCALL                                      0  $10     
         34        SEND_VAR                                                 $10
         35        DO_ICALL                                                 
   13    36        INIT_FCALL                                               'var_dump'
         37        INIT_FCALL                                               'firstnwords'
         38        SEND_VAL                                                 'hello'
         39        DO_FCALL                                      0  $12     
         40        SEND_VAR                                                 $12
         41        DO_ICALL                                                 
   14    42        INIT_FCALL                                               'var_dump'
         43        INIT_FCALL                                               'firstnwords'
         44        SEND_VAL                                                 'a'
         45        DO_FCALL                                      0  $14     
         46        SEND_VAR                                                 $14
         47        DO_ICALL                                                 
   15    48        INIT_FCALL                                               'var_dump'
         49        INIT_FCALL                                               'firstnwords'
         50        SEND_VAL                                                 ''
         51        DO_FCALL                                      0  $16     
         52        SEND_VAR                                                 $16
         53        DO_ICALL                                                 
         54      > RETURN                                                   1

Function firstnwords:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ocRD7
function name:  firstNWords
number of ops:  12
compiled vars:  !0 = $string, !1 = $limit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      3
    4     2        INIT_FCALL                                               'preg_replace'
          3        ROPE_INIT                                     3  ~3      '%2F%28%3F%3A%5CW%2A%5Cw%2B%29%7B'
          4        ROPE_ADD                                      1  ~3      ~3, !1
          5        ROPE_END                                      2  ~2      ~3, '%7D%5CK.%2A%2F'
          6        SEND_VAL                                                 ~2
          7        SEND_VAL                                                 ''
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $5      
         10      > RETURN                                                   $5
    5    11*     > RETURN                                                   null

End of function firstnwords

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.38 ms | 1080 KiB | 24 Q