Created
July 12, 2012 00:44
-
-
Save timgavin/3094787 to your computer and use it in GitHub Desktop.
Simple function to add smart apostrophes to the end of a string.
This file contains 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
// see if a string ends in 's' and add aprostrophes accordingly | |
// optionally adds an 's' to the end of a string depending if $count is supplied | |
function plural($str,$count=null) { | |
if($count != null) { | |
if($count == 0 || $count > 1) { | |
if(substr($str,-1) == 's') { | |
return $count.' '.$str; | |
} else { | |
return $count.' '.$str.'s'; | |
} | |
} else { | |
return $count.' '.$str; | |
} | |
} else { | |
if(substr($str,-1) == 's') { | |
return $str."'"; | |
} else { | |
return $str."'s"; | |
} | |
} | |
} | |
# determine whether to use 'are' or 'is' in a sentence | |
function are_is($count) { | |
if($count > 1) { | |
return 'are'; | |
} else { | |
return 'is'; | |
} | |
} | |
// usage | |
$name1 = 'Chris'; | |
$food1 = 'gyro'; | |
$total_food1 = 1; | |
$name2 = 'Tim'; | |
$food2 = 'beer'; | |
$total_food2 = 20; | |
echo 'That salad is '.plural($name1).'<br>'; | |
echo 'There '.are_is($total_food1).' '.plural($food1,$total_food1).'<br><br>'; | |
echo 'That pizza is '.plural($name2).'<br>'; | |
echo 'There '.are_is($total_food2).' '.plural($food2,$total_food2).'<br>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment