From 7c6a5891c25bfdd392ed6317fd8f87b58386f030 Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Fri, 11 Mar 2011 17:33:05 -0500 Subject: Changed the 'plural' function so that it doesn't ruin the captalization of your string. It also take into consideration acronyms which are all caps. --- system/helpers/inflector_helper.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 4cd7486b4..3042202cb 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -82,18 +82,17 @@ if ( ! function_exists('plural')) { function plural($str, $force = FALSE) { - $str = strtolower(trim($str)); $end = substr($str, -1); - if ($end == 'y') + if (preg_match('/y/i',$end)) { // Y preceded by vowel => regular plural - $vowels = array('a', 'e', 'i', 'o', 'u'); + $vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'); $str = in_array(substr($str, -2, 1), $vowels) ? $str.'s' : substr($str, 0, -1).'ies'; } - elseif ($end == 'h') + elseif (preg_match('/h/i',$end)) { - if (substr($str, -2) == 'ch' OR substr($str, -2) == 'sh') + if(preg_match('/^[c|s]h$/i',substr($str, -2))) { $str .= 'es'; } @@ -102,7 +101,7 @@ if ( ! function_exists('plural')) $str .= 's'; } } - elseif ($end == 's') + elseif (preg_match('/s/i',$end)) { if ($force == TRUE) { -- cgit v1.2.3-24-g4f1b From af376a2df84123549293af846f827ff4da30bf5e Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Fri, 11 Mar 2011 17:46:53 -0500 Subject: Forgot to trim the string first. --- system/helpers/inflector_helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index 3042202cb..e1cd66be0 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -81,7 +81,8 @@ if ( ! function_exists('singular')) if ( ! function_exists('plural')) { function plural($str, $force = FALSE) - { + { + $str = trim($str); $end = substr($str, -1); if (preg_match('/y/i',$end)) -- cgit v1.2.3-24-g4f1b From 2f620fe804ae5b7f4f20adc70ceaee1cf616a655 Mon Sep 17 00:00:00 2001 From: Kyle Farris Date: Fri, 11 Mar 2011 18:34:07 -0500 Subject: Fixed the capitalization "bug" in the singular function as well. --- system/helpers/inflector_helper.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/system/helpers/inflector_helper.php b/system/helpers/inflector_helper.php index e1cd66be0..c7c113b8a 100644 --- a/system/helpers/inflector_helper.php +++ b/system/helpers/inflector_helper.php @@ -41,20 +41,22 @@ if ( ! function_exists('singular')) { function singular($str) { - $str = strtolower(trim($str)); + $str = trim($str); $end = substr($str, -3); - - if ($end == 'ies') + + $str = preg_replace('/(.*)?([s|c]h)es/i','$1$2',$str); + + if (strtolower($end) == 'ies') { - $str = substr($str, 0, strlen($str)-3).'y'; + $str = substr($str, 0, strlen($str)-3).(preg_match('/[a-z]/',$end) ? 'y' : 'Y'); } - elseif ($end == 'ses') + elseif (strtolower($end) == 'ses') { $str = substr($str, 0, strlen($str)-2); } else { - $end = substr($str, -1); + $end = strtolower(substr($str, -1)); if ($end == 's') { -- cgit v1.2.3-24-g4f1b