3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Thesaurus { protected $synonyms = array( array('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[ $index ]); print_r($synonym); exit; 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) { 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
Jump found. Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
Jump found. Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
Jump found. Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 20
Jump found. Position 1 = -2
Branch analysis from position: 20
filename:       /in/Ggbt9
function name:  (null)
number of ops:  22
compiled vars:  !0 = $transformer, !1 = $index
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  E >   NOP                                                      
  23     1        NOP                                                      
  45     2        NEW                                              $4      :-6
         3        NEW                                              $5      :-4
         4        DO_FCALL                                      0          
         5        SEND_VAR_NO_REF                               0          $5
         6        DO_FCALL                                      0          
         7        ASSIGN                                                   !0, $4
  47     8        INIT_FCALL                                               'range'
         9        SEND_VAL                                                 1
        10        SEND_VAL                                                 20
        11        DO_ICALL                                         $9      
        12      > FE_RESET_R                                       $10     $9, ->20
        13    > > FE_FETCH_R                                               $10, !1, ->20
  48    14    >   INIT_METHOD_CALL                                         !0, 'transform'
        15        SEND_VAL_EX                                              'This+is+my+great+description.+It+really+is+wonderful.+Look+how+wonderful+it+is.'
        16        DO_FCALL                                      0  $11     
        17        ECHO                                                     $11
        18        ECHO                                                     '%0A'
        19      > JMP                                                      ->13
        20    >   FE_FREE                                                  $10
        21      > RETURN                                                   1

Class Thesaurus:
Function lookup:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
Jump found. Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
Jump found. Position 1 = 13, Position 2 = 24
Branch analysis from position: 13
Jump found. Position 1 = -2
Branch analysis from position: 24
Jump found. Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 25
Jump found. Position 1 = -2
Branch analysis from position: 25
filename:       /in/Ggbt9
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    >   UNSET_DIM                                                !1, !2
  14    14        INIT_FCALL                                               'print_r'
        15        SEND_VAR                                                 !1
        16        DO_ICALL                                                 
  15    17      > EXIT                                                     
  16    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
        24    > > JMP                                                      ->3
        25    >   FE_FREE                                                  $4
  19    26      > RETURN                                                   !0
  20    27*     > RETURN                                                   null

End of function lookup

End of class Thesaurus.

Class Transformer:
Function __construct:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/Ggbt9
function name:  __construct
number of ops:  4
compiled vars:  !0 = $thesaurus
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  26     0  E >   RECV                                             !0      
  27     1        ASSIGN_OBJ                                               'thesaurus'
         2        OP_DATA                                                  !0
  28     3      > RETURN                                                   null

End of function __construct

Function transform:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = 6, Position 2 = 18
Branch analysis from position: 6
Jump found. Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
Jump found. Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 18
Jump found. Position 1 = -2
Branch analysis from position: 18
filename:       /in/Ggbt9
function name:  transform
number of ops:  21
compiled vars:  !0 = $string, !1 = $word
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  29     0  E >   RECV                                             !0      
  30     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
  31     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                               4          $5
        14        SEND_VAR_EX                                              !0
        15        DO_FCALL                                      0  $6      
        16        ASSIGN                                                   !0, $6
        17      > JMP                                                      ->6
        18    >   FE_FREE                                                  $3
  33    19      > RETURN                                                   !0
  34    20*     > RETURN                                                   null

End of function transform

Function replace:
Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/Ggbt9
function name:  replace
number of ops:  17
compiled vars:  !0 = $needle, !1 = $replacement, !2 = $haystack
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  35     0  E >   RECV                                             !0      
         1        RECV                                             !1      
         2        RECV                                             !2      
  36     3        INIT_FCALL                                               'preg_replace'
  37     4        INIT_FCALL                                               'preg_quote'
         5        SEND_VAR                                                 !0
         6        SEND_VAL                                                 '%7E'
         7        DO_ICALL                                         $3      
         8        CONCAT                                           ~4      '%7E', $3
         9        CONCAT                                           ~5      ~4, '%7Ei'
        10        SEND_VAL                                                 ~5
        11        SEND_VAR                                                 !1
        12        SEND_VAR                                                 !2
  40    13        SEND_VAL                                                 1
        14        DO_ICALL                                         $6      
        15      > RETURN                                                   $6
  42    16*     > RETURN                                                   null

End of function replace

End of class Transformer.

Generated using Vulcan Logic Dumper, using php 7.3.0