diff options
author | Andrey Andreev <narf@devilix.net> | 2016-10-21 17:04:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-21 17:04:12 +0200 |
commit | 4de4f4c88ea49db3867882f21b727e953ca1a590 (patch) | |
tree | 3c1bf5eab322ca05088be8cbed2e5aa3d9362352 | |
parent | 5d05372f4f59d27fdd93249d813970fcf181a4af (diff) | |
parent | 25dc0937dd27c6d3c9f4d4483a857c218d6a9dad (diff) |
Merge pull request #4862 from gxgpet/remote-develop
New function for Inflector helper: ordinal_format()
-rw-r--r-- | system/helpers/inflector_helper.php | 39 | ||||
-rw-r--r-- | user_guide_src/source/helpers/inflector_helper.rst | 18 |
2 files changed, 56 insertions, 1 deletions
diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 6dc3b5030..83bf28098 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -273,3 +273,42 @@ if ( ! function_exists('is_countable')) ); } } + +// ------------------------------------------------------------------------ + +if ( ! function_exists('ordinal_format')) +{ + /** + * Returns the English ordinal numeral for a given number + * + * @param int $number + * @return string + */ + function ordinal_format($number) + { + if ( ! ctype_digit((string) $number) OR $number < 1) + { + return $number; + } + + $last_digit = array( + 0 => 'th', + 1 => 'st', + 2 => 'nd', + 3 => 'rd', + 4 => 'th', + 5 => 'th', + 6 => 'th', + 7 => 'th', + 8 => 'th', + 9 => 'th' + ); + + if (($number % 100) >= 11 && ($number % 100) <= 13) + { + return $number.'th'; + } + + return $number.$last_digit[$number % 10]; + } +} diff --git a/user_guide_src/source/helpers/inflector_helper.rst b/user_guide_src/source/helpers/inflector_helper.rst index df0c568c0..76cce6f4d 100644 --- a/user_guide_src/source/helpers/inflector_helper.rst +++ b/user_guide_src/source/helpers/inflector_helper.rst @@ -93,4 +93,20 @@ The following functions are available: Checks if the given word has a plural version. Example:: - is_countable('equipment'); // Returns FALSE
\ No newline at end of file + is_countable('equipment'); // Returns FALSE + +.. php:function:: ordinal_format($number) + + :param int $number: non-negative natural number to be converted + :returns: Ordinal numeral for given number or original value on failure + :rtype: 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 |