Created
May 1, 2011 13:29
-
-
Save ronan-gloo/950500 to your computer and use it in GitHub Desktop.
Fuel Inflector suggestion
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
<?php | |
/** | |
* plural_or_singular function. | |
* Call singularize or pluralize method by counting a num or an array. | |
* | |
* | |
* @param int $count (default: 1) string, int or array: the value to count | |
* @param string $str (default: '') string to pluralize / singularize | |
* @param bool $w_count (default: false) Determine if the function returns the count | |
* @param string $sep (default: ' ') count - string separator | |
* @return void | |
*/ | |
public static function plural_or_singular($count = 1, $str = '', $w_count = false, $sep = ' ') | |
{ | |
if (is_array($count)) | |
{ | |
$count = count($count); | |
} | |
$n = ($w_count === true) ? $count.$sep : ''; | |
return ($count > 1) ? $n.static::pluralize($str) : $n.static::singularize($str); | |
} | |
/* Examples */ | |
$a = array(1,2,3,4); | |
echo $inflector->plural_or_singular($a, 'number', true); | |
// returns "4 numbers" | |
$n = "1"; | |
echo $inflector->plural_or_singular($n, 'numbers') | |
// returns "number" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment