Changeset 63

Show
Ignore:
Timestamp:
05/22/2007 02:22:50 PM (19 months ago)
Author:
Shadowhand
Message:

Fixing #35, replaced plural() with a more intelligent solution, fixed singular() to work with strings that end in "zes", "xes", or "ses".

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/inflector_helper.php

    r24 r63  
    3737 * @param       string 
    3838 * @return      str 
    39  */              
     39 */ 
    4040function singular($str) 
    4141{ 
    42         $str = strtolower(trim($str)); 
     42        $str = trim(rtrim($str)); 
    4343        $end = substr($str, -3); 
    44          
     44 
    4545        if ($end == 'ies') 
    4646        { 
    4747                $str = substr($str, 0, strlen($str)-3).'y'; 
    4848        } 
     49        elseif ($end == 'ses' || $end == 'zes' || $end == 'xes') 
     50        { 
     51                $str = substr($str, 0, strlen($str)-2); 
     52        } 
    4953        else 
    5054        { 
    5155                $end = substr($str, -1); 
    52                  
     56 
    5357                if ($end == 's') 
    5458                { 
     
    5660                } 
    5761        } 
    58          
     62 
    5963        return $str; 
    6064} 
     
    7074 * @param       string 
    7175 * @return      str 
    72  */              
     76 */ 
    7377function plural($str) 
    7478{ 
    75         $str = strtolower(trim($str)); 
     79        $str = trim(rtrim($str)); 
    7680        $end = substr($str, -1); 
     81        $low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE; 
    7782 
    78         if ($end == 'y') 
     83        if (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str)) 
    7984        { 
    80                 $str = substr($str, 0, strlen($str)-1).'ies'; 
     85                $end = 'es'; 
     86                $str .= ($low == FALSE) ? strtoupper($end) : $end; 
    8187        } 
    82         elseif ($end != 's') 
     88        elseif (preg_match('/[^aeiou]y$/i', $str)) 
    8389        { 
    84                 $str .= 's'; 
     90                $end = 'ies'; 
     91                $end = ($low == FALSE) ? strtoupper($end) : $end; 
     92                $str = substr_replace($str, $end, -1) 
     93        } 
     94        else 
     95        { 
     96                $end = 's'; 
     97                $str .= ($low == FALSE) ? strtoupper($end) : $end; 
    8598        } 
    8699 
    87         return $str;     
     100        return $str; 
    88101} 
    89102 
     
    98111 * @param       string 
    99112 * @return      str 
    100  */              
     113 */ 
    101114function camelize($str) 
    102 {                
     115{ 
    103116        $str = 'x'.strtolower(trim($str)); 
    104117        $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); 
     
    116129 * @param       string 
    117130 * @return      str 
    118  */              
     131 */ 
    119132function underscore($str) 
    120133{ 
     
    132145 * @param       string 
    133146 * @return      str 
    134  */              
     147 */ 
    135148function humanize($str) 
    136149{ 
    137150        return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str)))); 
    138151} 
    139          
     152 
    140153?>