CodeIgniter Forums
Contextual - Inflector helper add-on - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Contextual - Inflector helper add-on (/showthread.php?tid=25641)



Contextual - Inflector helper add-on - El Forum - 12-18-2009

[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;
}



Contextual - Inflector helper add-on - El Forum - 12-19-2009

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


Contextual - Inflector helper add-on - El Forum - 12-19-2009

[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);
}



Contextual - Inflector helper add-on - El Forum - 12-19-2009

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


Contextual - Inflector helper add-on - El Forum - 04-30-2010

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