3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StringUtil{ /** * Returns the English plural of the given phrase based on the value. * Note, there are certainly exceptions and if someone wants to white/black-list them, go ahead. * Some exceptions that do not work with this function noted by mickmackusa: * appendix, axis, fungus, deer, child, syllabus, louse, person, goose, bacterium * @see first version: https://3v4l.org/VJni0 * @see https://stackoverflow.com/a/34254595/1993494 (idea from this) * @param string $phrase The phrase to determine the plural form * @param float|int|array|object $value The value to determine whether the phrase should be plural or not * @return string The English plural form of the phrase based on the value */ public static function pluralEnglish(string $singularPhrase, float|int|array|object $value): string{ if ($value === 1){ return $singularPhrase; } if (is_countable($value)){ if (count($value) === 1){ return $singularPhrase; } }else if (is_object($value)){ //object that does not implement Countable return $singularPhrase; } //If the string doesn't even end with a letter, just return the phrase. if (!preg_match('/[a-zA-Z]$/', $singularPhrase)){ return $singularPhrase; } if (str_ends_with($singularPhrase, 'y')){ return substr($singularPhrase, 0, -1).'ies'; } //In English, words that end with these characters, you add "es" to make them plural. $es_append = ['x','z','s','ch','sh']; foreach($es_append as $end){ if (str_ends_with($singularPhrase, $end)){ return $singularPhrase.'es'; } } //It didn't fit any of the above criteria, so just add "s" return $singularPhrase.'s'; } } $ary = [ 'kibitz', 'miss', 'fox', 'box', 'item', 'god', 'fun', 'diety', 'touch', 'teach', 'watch', 'test', 'fire', ]; $count = 0; foreach($ary as $phrase){ echo StringUtil::pluralEnglish($phrase, $count)."\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/3eSF4
function name:  (null)
number of ops:  13
compiled vars:  !0 = $ary, !1 = $count, !2 = $phrase
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   ASSIGN                                                   !0, <array>
   63     1        ASSIGN                                                   !1, 0
   64     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                               $5, !2, ->11
   65     4    >   INIT_STATIC_METHOD_CALL                                  'StringUtil', 'pluralEnglish'
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $6      
          8        CONCAT                                           ~7      $6, '%0A'
          9        ECHO                                                     ~7
   64    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   66    12      > RETURN                                                   1

Class StringUtil:
Function pluralenglish:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 16
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 28
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 41
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 54
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 54
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 53
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 54
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 20
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/3eSF4
function name:  pluralEnglish
number of ops:  60
compiled vars:  !0 = $singularPhrase, !1 = $value, !2 = $es_append, !3 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        IS_IDENTICAL                                             !1, 1
          3      > JMPZ                                                     ~4, ->6
   16     4    >   VERIFY_RETURN_TYPE                                       !0
          5      > RETURN                                                   !0
   18     6    >   INIT_FCALL                                               'is_countable'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $5      
          9      > JMPZ                                                     $5, ->16
   19    10    >   COUNT                                            ~6      !1
         11        IS_IDENTICAL                                             ~6, 1
         12      > JMPZ                                                     ~7, ->15
   20    13    >   VERIFY_RETURN_TYPE                                       !0
         14      > RETURN                                                   !0
   18    15    > > JMP                                                      ->20
   22    16    >   TYPE_CHECK                                  256          !1
         17      > JMPZ                                                     ~8, ->20
   24    18    >   VERIFY_RETURN_TYPE                                       !0
         19      > RETURN                                                   !0
   28    20    >   INIT_FCALL                                               'preg_match'
         21        SEND_VAL                                                 '%2F%5Ba-zA-Z%5D%24%2F'
         22        SEND_VAR                                                 !0
         23        DO_ICALL                                         $9      
         24        BOOL_NOT                                         ~10     $9
         25      > JMPZ                                                     ~10, ->28
   29    26    >   VERIFY_RETURN_TYPE                                       !0
         27      > RETURN                                                   !0
   32    28    >   INIT_FCALL                                               'str_ends_with'
         29        SEND_VAR                                                 !0
         30        SEND_VAL                                                 'y'
         31        DO_ICALL                                         $11     
         32      > JMPZ                                                     $11, ->41
   33    33    >   INIT_FCALL                                               'substr'
         34        SEND_VAR                                                 !0
         35        SEND_VAL                                                 0
         36        SEND_VAL                                                 -1
         37        DO_ICALL                                         $12     
         38        CONCAT                                           ~13     $12, 'ies'
         39        VERIFY_RETURN_TYPE                                       ~13
         40      > RETURN                                                   ~13
   37    41    >   ASSIGN                                                   !2, <array>
   38    42      > FE_RESET_R                                       $15     !2, ->54
         43    > > FE_FETCH_R                                               $15, !3, ->54
   39    44    >   INIT_FCALL                                               'str_ends_with'
         45        SEND_VAR                                                 !0
         46        SEND_VAR                                                 !3
         47        DO_ICALL                                         $16     
         48      > JMPZ                                                     $16, ->53
   40    49    >   CONCAT                                           ~17     !0, 'es'
         50        VERIFY_RETURN_TYPE                                       ~17
         51        FE_FREE                                                  $15
         52      > RETURN                                                   ~17
   38    53    > > JMP                                                      ->43
         54    >   FE_FREE                                                  $15
   44    55        CONCAT                                           ~18     !0, 's'
         56        VERIFY_RETURN_TYPE                                       ~18
         57      > RETURN                                                   ~18
   45    58*       VERIFY_RETURN_TYPE                                       
         59*     > RETURN                                                   null

End of function pluralenglish

End of class StringUtil.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.14 ms | 1012 KiB | 17 Q