Welcome Guest, Not a member yet? Register   Sign In
Contextual - Inflector helper add-on
#1

[eluser]hotmeteor[/eluser]
Hey, I've created this little snippet as an addition to the Inflector helper. It takes a string and a count, and decides how to deal with it.

Code:
/**
* Contextual
*
* Takes a string and a number and decides how to deal with it.
*
* @access    public
* @param    string
* @return    str
*/
function contextual($str, $num)
{
    $str = strtolower(trim($str));
    if($num != 1)
    {
        $str = plural($str);
    }
    else
    {
        $str = singular($str);
    }
    return $str;
}
#2

[eluser]Johan André[/eluser]
Great!
I think wordpress has a helper like this...
#3

[eluser]helmutbjorg[/eluser]
This is great. Don't know why I didn't think of it!

However you can shorten this out it because both the plural and singular functions already trim and strtolower the $str var...
Code:
/**
* Contextual
*
* Takes a string and a number and decides how to deal with it.
*
* @access    public
* @param    string
* @return    str
*/
function contextual($str, $num)
{
    return ($num != 1) ? plural($str) : singular($str);
}
#4

[eluser]hotmeteor[/eluser]
Beauty, thanks.
#5

[eluser]milkboy[/eluser]
Thanks, that is extremely useful to me.




Theme © iAndrew 2016 - Forum software by © MyBB