3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Thesaurus { protected $synonyms = array( array('great', 'wonderful', 'amazing', 'fabulous', 'awesome', 'stunning'), array('look', 'see', 'observe') ); public function lookup($word) { foreach ($this->synonyms as $index => $synonym) { if (in_array(strtolower($word), $synonym)) { unset($synonym[ array_search($word, $synonym) ]); return $synonym[ array_rand($synonym) ]; } } return $word; } } class Transformer { protected $thesaurus; public function __construct(Thesaurus $thesaurus) { $this->thesaurus = $thesaurus; } public function transform($string) { foreach (str_word_count($string, 1) as $word) { $string = $this->replace($word, $this->thesaurus->lookup($word), $string); } return $string; } private function replace($needle, $replacement, $haystack) { (ctype_upper($needle[1])) ? ucfirst($replacement) : '' ; return preg_replace( '~' . preg_quote($needle, '~') . '~i', $replacement, $haystack, 1 ); } } $transformer = new Transformer(new Thesaurus); foreach (range(1, 20) as $index) { echo $transformer->transform('This is my great description. It really is wonderful. Look how wonderful it is.'), PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/hEMMc
function name:  (null)
number of ops:  20
compiled vars:  !0 = $transformer, !1 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   NEW                                              $2      'Transformer'
          1        NEW                                              $3      'Thesaurus'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $3
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   48     6        INIT_FCALL                                               'range'
          7        SEND_VAL                                                 1
          8        SEND_VAL                                                 20
          9        DO_ICALL                                         $7      
         10      > FE_RESET_R                                       $8      $7, ->18
         11    > > FE_FETCH_R                                               $8, !1, ->18
   49    12    >   INIT_METHOD_CALL                                         !0, 'transform'
         13        SEND_VAL_EX                                              'This+is+my+great+description.+It+really+is+wonderful.+Look+how+wonderful+it+is.'
         14        DO_FCALL                                      0  $9      
         15        ECHO                                                     $9
         16        ECHO                                                     '%0A'
   48    17      > JMP                                                      ->11
         18    >   FE_FREE                                                  $8
   50    19      > RETURN                                                   1

Class Thesaurus:
Function lookup:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 24
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/hEMMc
function name:  lookup
number of ops:  28
compiled vars:  !0 = $word, !1 = $synonym, !2 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        FETCH_OBJ_R                                      ~3      'synonyms'
          2      > FE_RESET_R                                       $4      ~3, ->25
          3    > > FE_FETCH_R                                       ~5      $4, !1, ->25
          4    >   ASSIGN                                                   !2, ~5
   11     5        INIT_FCALL                                               'in_array'
          6        INIT_FCALL                                               'strtolower'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $7      
          9        SEND_VAR                                                 $7
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $8      
         12      > JMPZ                                                     $8, ->24
   12    13    >   INIT_FCALL                                               'array_search'
         14        SEND_VAR                                                 !0
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $9      
         17        UNSET_DIM                                                !1, $9
   13    18        INIT_FCALL                                               'array_rand'
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $10     
         21        FETCH_DIM_R                                      ~11     !1, $10
         22        FE_FREE                                                  $4
         23      > RETURN                                                   ~11
   10    24    > > JMP                                                      ->3
         25    >   FE_FREE                                                  $4
   16    26      > RETURN                                                   !0
   17    27*     > RETURN                                                   null

End of function lookup

End of class Thesaurus.

Class Transformer:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hEMMc
function name:  __construct
number of ops:  4
compiled vars:  !0 = $thesaurus
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        ASSIGN_OBJ                                               'thesaurus'
          2        OP_DATA                                                  !0
   25     3      > RETURN                                                   null

End of function __construct

Function transform:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 18
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/hEMMc
function name:  transform
number of ops:  21
compiled vars:  !0 = $string, !1 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   27     1        INIT_FCALL                                               'str_word_count'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 1
          4        DO_ICALL                                         $2      
          5      > FE_RESET_R                                       $3      $2, ->18
          6    > > FE_FETCH_R                                               $3, !1, ->18
   28     7    >   INIT_METHOD_CALL                                         'replace'
          8        SEND_VAR_EX                                              !1
          9        FETCH_OBJ_R                                      ~4      'thesaurus'
         10        INIT_METHOD_CALL                                         ~4, 'lookup'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $5      
         13        SEND_VAR_NO_REF_EX                                       $5
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0  $6      
         16        ASSIGN                                                   !0, $6
   27    17      > JMP                                                      ->6
         18    >   FE_FREE                                                  $3
   30    19      > RETURN                                                   !0
   31    20*     > RETURN                                                   null

End of function transform

Function replace:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hEMMc
function name:  replace
number of ops:  29
compiled vars:  !0 = $needle, !1 = $replacement, !2 = $haystack
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   35     3        INIT_FCALL                                               'ctype_upper'
          4        FETCH_DIM_R                                      ~3      !0, 1
          5        SEND_VAL                                                 ~3
          6        DO_ICALL                                         $4      
          7      > JMPZ                                                     $4, ->13
          8    >   INIT_FCALL                                               'ucfirst'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $5      
         11        QM_ASSIGN                                        ~6      $5
         12      > JMP                                                      ->14
         13    >   QM_ASSIGN                                        ~6      ''
         14    >   FREE                                                     ~6
   37    15        INIT_FCALL                                               'preg_replace'
   38    16        INIT_FCALL                                               'preg_quote'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 '%7E'
         19        DO_ICALL                                         $7      
         20        CONCAT                                           ~8      '%7E', $7
         21        CONCAT                                           ~9      ~8, '%7Ei'
         22        SEND_VAL                                                 ~9
   39    23        SEND_VAR                                                 !1
   40    24        SEND_VAR                                                 !2
   41    25        SEND_VAL                                                 1
         26        DO_ICALL                                         $10     
         27      > RETURN                                                   $10
   43    28*     > RETURN                                                   null

End of function replace

End of class Transformer.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.68 ms | 1396 KiB | 33 Q