Inflector Helper

The Inflector Helper file contains functions that permits you to change English words to plural, singular, camel case, etc.

Loading this Helper

This helper is loaded using the following code:

$this->load->helper('inflector');

Available Functions

The following functions are available:

singular($str)
Parameters:
  • $str (string) – Input string
Returns:

A singular word

Return type:

string

Changes a plural word to singular. Example:

echo singular('dogs'); // Prints 'dog'
plural($str)
Parameters:
  • $str (string) – Input string
Returns:

A plural word

Return type:

string

Changes a singular word to plural. Example:

echo plural('dog'); // Prints 'dogs'
camelize($str)
Parameters:
  • $str (string) – Input string
Returns:

Camelized string

Return type:

string

Changes a string of words separated by spaces or underscores to camel case. Example:

echo camelize('my_dog_spot'); // Prints 'myDogSpot'
underscore($str)
Parameters:
  • $str (string) – Input string
Returns:

String containing underscores instead of spaces

Return type:

string

Takes multiple words separated by spaces and underscores them. Example:

echo underscore('my dog spot'); // Prints 'my_dog_spot'
humanize($str[, $separator = '_'])
Parameters:
  • $str (string) – Input string
  • $separator (string) – Input separator
Returns:

Humanized string

Return type:

string

Takes multiple words separated by underscores and adds spaces between them. Each word is capitalized.

Example:

echo humanize('my_dog_spot'); // Prints 'My Dog Spot'

To use dashes instead of underscores:

echo humanize('my-dog-spot', '-'); // Prints 'My Dog Spot'
word_is_countable($word)
Parameters:
  • $word (string) – Input string
Returns:

TRUE if the word is countable or FALSE if not

Return type:

bool

Checks if the given word has a plural version. Example:

word_is_countable('equipment'); // Returns FALSE

Note

This function used to be called is_countable() in in previous CodeIgniter versions.

ordinal_format($number)
Parameters:
  • $number (int) – non-negative natural number to be converted
Returns:

Ordinal numeral for given number or original value on failure

Return type:

string

Returns the ordinal numeral (1st, 2nd, 3rd etc.) for a non-negative natural number. If the input is not a natural number greater than 0, the function will return the original value. Examples:

echo ordinal_format(1); // Returns 1st
echo ordinal_format(3); // Returns 3rd
echo ordinal_format(21); // Returns 21st
echo ordinal_format(102); // Returns 102nd
echo ordinal_format(-5); // Invalid input, will return -5