3v4l.org

run code in 300+ PHP versions simultaneously
<?php function is_allcaps($string) { $last_letter = mb_substr($string, -1, 1, 'UTF-8'); return $last_letter === mb_strtoupper($last_letter, 'UTF-8'); // otherwise use cytpe_upper() and setlocale() } function plural_to_singular($string) { // quick return of "untouchables" if(preg_match('~^(?:[oó]culos|parab[eé]ns|f[eé]rias)$~iu', $string)) { return $string; } $regex_map = [ '~[õã]es$~iu' => 'ão', '~(?:[áó].*e|[eé])is$~iu' => 'el', '~[^eé]\Kis$~iu' => 'l', '~ns$~iu' => 'm', '~eses$~iu' => 'ês', '~(?:[rzs]\Ke)?s$~iu' => '' ]; foreach ($regex_map as $pattern => $replacement) { $singular = preg_replace($pattern, $replacement, $string, 1, $count); if ($count) { return is_allcaps($string) ? mb_strtoupper($singular) : $singular; } } return $string; } $words = array( 'óculos' => 'óculos', 'papéis' => 'papel', 'anéis' => 'anel', 'PASTEIS' => 'PASTEL', 'CAMIÕES' => 'CAMIÃO', 'rodas' => 'roda', 'cães' => 'cão', 'meses' => 'mês', 'vezes' => 'vez', 'luzes' => 'luz', 'cristais' => 'cristal', 'canções' => 'canção', 'nuvens' => 'nuvem', 'alemães' => 'alemão' ); foreach($words as $plural => $singular) { echo "$plural => $singular = " , plural_to_singular($plural) , "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 15
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/vpHGS
function name:  (null)
number of ops:  17
compiled vars:  !0 = $words, !1 = $singular, !2 = $plural
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   ASSIGN                                                   !0, <array>
   55     1      > FE_RESET_R                                       $4      !0, ->15
          2    > > FE_FETCH_R                                       ~5      $4, !1, ->15
          3    >   ASSIGN                                                   !2, ~5
   57     4        ROPE_INIT                                     4  ~8      !2
          5        ROPE_ADD                                      1  ~8      ~8, '+%3D%3E+'
          6        ROPE_ADD                                      2  ~8      ~8, !1
          7        ROPE_END                                      3  ~7      ~8, '+%3D+'
          8        ECHO                                                     ~7
          9        INIT_FCALL                                               'plural_to_singular'
         10        SEND_VAR                                                 !2
         11        DO_FCALL                                      0  $10     
         12        ECHO                                                     $10
         13        ECHO                                                     '%0A'
   55    14      > JMP                                                      ->2
         15    >   FE_FREE                                                  $4
   58    16      > RETURN                                                   1

Function is_allcaps:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vpHGS
function name:  is_allcaps
number of ops:  15
compiled vars:  !0 = $string, !1 = $last_letter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    4     1        INIT_FCALL                                               'mb_substr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 -1
          4        SEND_VAL                                                 1
          5        SEND_VAL                                                 'UTF-8'
          6        DO_ICALL                                         $2      
          7        ASSIGN                                                   !1, $2
    5     8        INIT_FCALL                                               'mb_strtoupper'
          9        SEND_VAR                                                 !1
         10        SEND_VAL                                                 'UTF-8'
         11        DO_ICALL                                         $4      
         12        IS_IDENTICAL                                     ~5      !1, $4
         13      > RETURN                                                   ~5
    7    14*     > RETURN                                                   null

End of function is_allcaps

Function plural_to_singular:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 33
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 33
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 32
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 29
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
filename:       /in/vpHGS
function name:  plural_to_singular
number of ops:  36
compiled vars:  !0 = $string, !1 = $regex_map, !2 = $replacement, !3 = $pattern, !4 = $singular, !5 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   12     1        INIT_FCALL                                               'preg_match'
          2        SEND_VAL                                                 '%7E%5E%28%3F%3A%5Bo%C3%B3%5Dculos%7Cparab%5Be%C3%A9%5Dns%7Cf%5Be%C3%A9%5Drias%29%24%7Eiu'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5      > JMPZ                                                     $6, ->7
   14     6    > > RETURN                                                   !0
   17     7    >   ASSIGN                                                   !1, <array>
   26     8      > FE_RESET_R                                       $8      !1, ->33
          9    > > FE_FETCH_R                                       ~9      $8, !2, ->33
         10    >   ASSIGN                                                   !3, ~9
   28    11        INIT_FCALL                                               'preg_replace'
         12        SEND_VAR                                                 !3
         13        SEND_VAR                                                 !2
         14        SEND_VAR                                                 !0
         15        SEND_VAL                                                 1
         16        SEND_REF                                                 !5
         17        DO_ICALL                                         $11     
         18        ASSIGN                                                   !4, $11
   29    19      > JMPZ                                                     !5, ->32
   31    20    >   INIT_FCALL                                               'is_allcaps'
         21        SEND_VAR                                                 !0
         22        DO_FCALL                                      0  $13     
         23      > JMPZ                                                     $13, ->29
         24    >   INIT_FCALL                                               'mb_strtoupper'
         25        SEND_VAR                                                 !4
         26        DO_ICALL                                         $14     
         27        QM_ASSIGN                                        ~15     $14
         28      > JMP                                                      ->30
         29    >   QM_ASSIGN                                        ~15     !4
         30    >   FE_FREE                                                  $8
         31      > RETURN                                                   ~15
   26    32    > > JMP                                                      ->9
         33    >   FE_FREE                                                  $8
   35    34      > RETURN                                                   !0
   36    35*     > RETURN                                                   null

End of function plural_to_singular

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.89 ms | 1014 KiB | 19 Q