Created
June 3, 2016 13:15
-
-
Save Balamir/28aadcc029dde357bdeb7517543dc21d to your computer and use it in GitHub Desktop.
English word plurization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| original : http://stackoverflow.com/a/34254595 | |
| */ | |
| function getPluralPrase( $phrase, $value ) { | |
| $plural=''; | |
| if( $value > 1 ) { | |
| for( $i = 0; $i < strlen( $phrase ); $i++ ) { | |
| if( $i == strlen( $phrase ) -1 ) { | |
| $plural .= ( $phrase[$i] == 'y' )? 'ies' : ( ( $phrase[$i] == 's' || $phrase[$i] == 'x' || $phrase[$i] == 'z' || $phrase[$i] == 'ch' || $phrase[$i] == 'sh' ) ? $phrase[$i] . 'es' : $phrase[$i] . 's' ); | |
| } else { | |
| $plural .= $phrase[$i]; | |
| } | |
| } | |
| return $plural; | |
| } | |
| return $phrase; | |
| } | |
| echo getPluralPrase( 'post', 1 ) . "\n"; //singular -> post | |
| echo getPluralPrase( 'post', 2 ) . "\n"; //plural -> posts | |
| echo getPluralPrase( 'summary', 1 ) . "\n"; //singular -> summary | |
| echo getPluralPrase( 'summary', 2 ) . "\n"; //plural -> summaries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment