Skip to content

Instantly share code, notes, and snippets.

@Balamir
Created June 3, 2016 13:15
Show Gist options
  • Select an option

  • Save Balamir/28aadcc029dde357bdeb7517543dc21d to your computer and use it in GitHub Desktop.

Select an option

Save Balamir/28aadcc029dde357bdeb7517543dc21d to your computer and use it in GitHub Desktop.
English word plurization
<?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