3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(E_ALL); mb_internal_encoding('UTF-8'); $text1 = "Кажется, нас обнаружили! Надо срочно уходить отсюда, пока не поздно. Бежим же скорее!"; $text2 = "Ну, прости меня! Не хотела я тебе зла сделать; да в себе не вольна была. Что говорила, что делала, себя не помнила."; $text3 = "Идет гражданская война. Космические корабли повстанцев, наносящие удар с тайной базы, одержали первую победу, в схватке со зловещей Галактической Империей."; function makeFirstCharLowerCase(string $word): string { return mb_strtolower(mb_substr($word, 0, 1)) . mb_substr($word, 1); } function makeFirstCharUpperCase(string $word): string { return mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1); } /** * @return array<string> * @throws Exception */ function splitTextToSentences(string $text): array { $sentences = preg_split('/[.!?]/', $text, -1, PREG_SPLIT_NO_EMPTY); if (false === $sentences) { throw new Exception(preg_last_error_msg()); } return $sentences; } /** * @return array<string> * @throws Exception */ function splitSentenceToWords(string $sentence): array { $words = preg_split('/[-,;:\s]/', $sentence, -1, PREG_SPLIT_NO_EMPTY); if (false === $words) { throw new Exception(preg_last_error_msg()); } return $words; } /** * @param array<string> $words * @return array<string> */ function reverseWords(array $words): array { return array_reverse($words); } /** * @throws Exception */ function makeTextInYodaStyle(string $text): string { $sentences = splitTextToSentences($text); $result = ''; foreach ($sentences as $sentence) { $words = splitSentenceToWords($sentence); $reversedWords = reverseWords($words); $reversedWords[0] = makeFirstCharUpperCase($reversedWords[0]); $reversedWords[count($reversedWords) - 1] = makeFirstCharLowerCase($reversedWords[count($reversedWords) - 1]); $result .= implode(' ', $reversedWords) . '. '; } return $result; } $texts = [$text1, $text2, $text3]; $yodaText = ''; try { foreach ($texts as $text) { $yodaText = makeTextInYodaStyle($text); echo "Йода говорит: $yodaText\n"; } } catch (Exception $e) { echo "Произошла ошибка: " . $e->getMessage(); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 25
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 25
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Found catch point at position: 27
Branch analysis from position: 27
2 jumps found. (Code = 107) Position 1 = 28, Position 2 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/la0hi
function name:  (null)
number of ops:  33
compiled vars:  !0 = $text1, !1 = $text2, !2 = $text3, !3 = $texts, !4 = $yodaText, !5 = $text, !6 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 32767
          2        DO_ICALL                                                 
    4     3        INIT_FCALL                                               'mb_internal_encoding'
          4        SEND_VAL                                                 'UTF-8'
          5        DO_ICALL                                                 
    6     6        ASSIGN                                                   !0, '%D0%9A%D0%B0%D0%B6%D0%B5%D1%82%D1%81%D1%8F%2C+%D0%BD%D0%B0%D1%81+%D0%BE%D0%B1%D0%BD%D0%B0%D1%80%D1%83%D0%B6%D0%B8%D0%BB%D0%B8%21+%D0%9D%D0%B0%D0%B4%D0%BE+%D1%81%D1%80%D0%BE%D1%87%D0%BD%D0%BE+%D1%83%D1%85%D0%BE%D0%B4%D0%B8%D1%82%D1%8C+%D0%BE%D1%82%D1%81%D1%8E%D0%B4%D0%B0%2C+%D0%BF%D0%BE%D0%BA%D0%B0+%D0%BD%D0%B5+%D0%BF%D0%BE%D0%B7%D0%B4%D0%BD%D0%BE.+%D0%91%D0%B5%D0%B6%D0%B8%D0%BC+%D0%B6%D0%B5+%D1%81%D0%BA%D0%BE%D1%80%D0%B5%D0%B5%21'
    7     7        ASSIGN                                                   !1, '%D0%9D%D1%83%2C+%D0%BF%D1%80%D0%BE%D1%81%D1%82%D0%B8+%D0%BC%D0%B5%D0%BD%D1%8F%21+%D0%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0%B5%D0%B4%D1%83%2C+%0A%D0%B2+%D1%81%D1%85%D0%B2%D0%B0%D1%82%D0%BA%D0%B5+%D1%81%D0%BE+%D0%B7%D0%BB%D0%BE%D0%B2%D0%B5%D1%89%D0%B5%D0%B9+%D0%93%D0%B0%D0%BB%D0%B0%D0%BA%D1%82%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%BE%D0%B9+%D0%98%D0%BC%D0%BF%D0%B5%D1%80%D0%B8%D0%B5%D0%B9.'
   75     9        INIT_ARRAY                                       ~12     !0
         10        ADD_ARRAY_ELEMENT                                ~12     !1
         11        ADD_ARRAY_ELEMENT                                ~12     !2
         12        ASSIGN                                                   !3, ~12
   76    13        ASSIGN                                                   !4, ''
   79    14      > FE_RESET_R                                       $15     !3, ->25
         15    > > FE_FETCH_R                                               $15, !5, ->25
   80    16    >   INIT_FCALL                                               'maketextinyodastyle'
         17        SEND_VAR                                                 !5
         18        DO_FCALL                                      0  $16     
         19        ASSIGN                                                   !4, $16
   81    20        ROPE_INIT                                     3  ~19     '%D0%99%D0%BE%D0%B4%D0%B0+%D0%B3%D0%BE%D0%B2%D0%BE%D1%80%D0%B8%D1%82%3A+'
         21        ROPE_ADD                                      1  ~19     ~19, !4
         22        ROPE_END                                      2  ~18     ~19, '%0A'
         23        ECHO                                                     ~18
   79    24      > JMP                                                      ->15
         25    >   FE_FREE                                                  $15
         26      > JMP                                                      ->32
   83    27  E > > CATCH                                       last         'Exception'
   84    28    >   INIT_METHOD_CALL                                         !6, 'getMessage'
         29        DO_FCALL                                      0  $21     
         30        CONCAT                                           ~22     '%D0%9F%D1%80%D0%BE%D0%B8%D0%B7%D0%BE%D1%88%D0%BB%D0%B0+%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D0%B0%3A+', $21
         31        ECHO                                                     ~22
   85    32    > > RETURN                                                   1

Function makefirstcharlowercase:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/la0hi
function name:  makeFirstCharLowerCase
number of ops:  18
compiled vars:  !0 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'mb_strtolower'
          2        INIT_FCALL                                               'mb_substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        SEND_VAL                                                 1
          6        DO_ICALL                                         $1      
          7        SEND_VAR                                                 $1
          8        DO_ICALL                                         $2      
          9        INIT_FCALL                                               'mb_substr'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 1
         12        DO_ICALL                                         $3      
         13        CONCAT                                           ~4      $2, $3
         14        VERIFY_RETURN_TYPE                                       ~4
         15      > RETURN                                                   ~4
   15    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function makefirstcharlowercase

Function makefirstcharuppercase:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/la0hi
function name:  makeFirstCharUpperCase
number of ops:  18
compiled vars:  !0 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   19     1        INIT_FCALL                                               'mb_strtoupper'
          2        INIT_FCALL                                               'mb_substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        SEND_VAL                                                 1
          6        DO_ICALL                                         $1      
          7        SEND_VAR                                                 $1
          8        DO_ICALL                                         $2      
          9        INIT_FCALL                                               'mb_substr'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 1
         12        DO_ICALL                                         $3      
         13        CONCAT                                           ~4      $2, $3
         14        VERIFY_RETURN_TYPE                                       ~4
         15      > RETURN                                                   ~4
   20    16*       VERIFY_RETURN_TYPE                                       
         17*     > RETURN                                                   null

End of function makefirstcharuppercase

Function splittexttosentences:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/la0hi
function name:  splitTextToSentences
number of ops:  20
compiled vars:  !0 = $text, !1 = $sentences
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   28     1        INIT_FCALL                                               'preg_split'
          2        SEND_VAL                                                 '%2F%5B.%21%3F%5D%2F'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 -1
          5        SEND_VAL                                                 1
          6        DO_ICALL                                         $2      
          7        ASSIGN                                                   !1, $2
   29     8        TYPE_CHECK                                    4          !1
          9      > JMPZ                                                     ~4, ->16
   30    10    >   NEW                                              $5      'Exception'
         11        INIT_FCALL                                               'preg_last_error_msg'
         12        DO_ICALL                                         $6      
         13        SEND_VAR_NO_REF_EX                                       $6
         14        DO_FCALL                                      0          
         15      > THROW                                         0          $5
   32    16    >   VERIFY_RETURN_TYPE                                       !1
         17      > RETURN                                                   !1
   33    18*       VERIFY_RETURN_TYPE                                       
         19*     > RETURN                                                   null

End of function splittexttosentences

Function splitsentencetowords:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/la0hi
function name:  splitSentenceToWords
number of ops:  20
compiled vars:  !0 = $sentence, !1 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
   41     1        INIT_FCALL                                               'preg_split'
          2        SEND_VAL                                                 '%2F%5B-%2C%3B%3A%5Cs%5D%2F'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 -1
          5        SEND_VAL                                                 1
          6        DO_ICALL                                         $2      
          7        ASSIGN                                                   !1, $2
   42     8        TYPE_CHECK                                    4          !1
          9      > JMPZ                                                     ~4, ->16
   43    10    >   NEW                                              $5      'Exception'
         11        INIT_FCALL                                               'preg_last_error_msg'
         12        DO_ICALL                                         $6      
         13        SEND_VAR_NO_REF_EX                                       $6
         14        DO_FCALL                                      0          
         15      > THROW                                         0          $5
   45    16    >   VERIFY_RETURN_TYPE                                       !1
         17      > RETURN                                                   !1
   46    18*       VERIFY_RETURN_TYPE                                       
         19*     > RETURN                                                   null

End of function splitsentencetowords

Function reversewords:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/la0hi
function name:  reverseWords
number of ops:  8
compiled vars:  !0 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        INIT_FCALL                                               'array_reverse'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        VERIFY_RETURN_TYPE                                       $1
          5      > RETURN                                                   $1
   55     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function reversewords

Function maketextinyodastyle:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 39
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 39
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/la0hi
function name:  makeTextInYodaStyle
number of ops:  44
compiled vars:  !0 = $text, !1 = $sentences, !2 = $result, !3 = $sentence, !4 = $words, !5 = $reversedWords
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   63     1        INIT_FCALL                                               'splittexttosentences'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $6      
          4        ASSIGN                                                   !1, $6
   64     5        ASSIGN                                                   !2, ''
   65     6      > FE_RESET_R                                       $9      !1, ->39
          7    > > FE_FETCH_R                                               $9, !3, ->39
   66     8    >   INIT_FCALL                                               'splitsentencetowords'
          9        SEND_VAR                                                 !3
         10        DO_FCALL                                      0  $10     
         11        ASSIGN                                                   !4, $10
   67    12        INIT_FCALL                                               'reversewords'
         13        SEND_VAR                                                 !4
         14        DO_FCALL                                      0  $12     
         15        ASSIGN                                                   !5, $12
   68    16        INIT_FCALL                                               'makefirstcharuppercase'
         17        FETCH_DIM_R                                      ~15     !5, 0
         18        SEND_VAL                                                 ~15
         19        DO_FCALL                                      0  $16     
         20        ASSIGN_DIM                                               !5, 0
         21        OP_DATA                                                  $16
   69    22        COUNT                                            ~17     !5
         23        SUB                                              ~18     ~17, 1
         24        INIT_FCALL                                               'makefirstcharlowercase'
         25        COUNT                                            ~20     !5
         26        SUB                                              ~21     ~20, 1
         27        FETCH_DIM_R                                      ~22     !5, ~21
         28        SEND_VAL                                                 ~22
         29        DO_FCALL                                      0  $23     
         30        ASSIGN_DIM                                               !5, ~18
         31        OP_DATA                                                  $23
   70    32        INIT_FCALL                                               'implode'
         33        SEND_VAL                                                 '+'
         34        SEND_VAR                                                 !5
         35        DO_ICALL                                         $24     
         36        CONCAT                                           ~25     $24, '.+'
         37        ASSIGN_OP                                     8          !2, ~25
   65    38      > JMP                                                      ->7
         39    >   FE_FREE                                                  $9
   72    40        VERIFY_RETURN_TYPE                                       !2
         41      > RETURN                                                   !2
   73    42*       VERIFY_RETURN_TYPE                                       
         43*     > RETURN                                                   null

End of function maketextinyodastyle

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
182.67 ms | 1028 KiB | 28 Q