CodeIgniter installed
[living-lab-site.git] / system / helpers / inflector_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * CodeIgniter Inflector Helpers
20  *
21  * @package             CodeIgniter
22  * @subpackage  Helpers
23  * @category    Helpers
24  * @author              ExpressionEngine Dev Team
25  * @link                http://codeigniter.com/user_guide/helpers/directory_helper.html
26  */
27
28
29 // --------------------------------------------------------------------
30
31 /**
32  * Singular
33  *
34  * Takes a plural word and makes it singular
35  *
36  * @access      public
37  * @param       string
38  * @return      str
39  */
40 if ( ! function_exists('singular'))
41 {
42         function singular($str)
43         {
44                 $str = trim($str);
45                 $end = substr($str, -3);
46         
47         $str = preg_replace('/(.*)?([s|c]h)es/i','$1$2',$str);
48         
49                 if (strtolower($end) == 'ies')
50                 {
51                         $str = substr($str, 0, strlen($str)-3).(preg_match('/[a-z]/',$end) ? 'y' : 'Y');
52                 }
53                 elseif (strtolower($end) == 'ses')
54                 {
55                         $str = substr($str, 0, strlen($str)-2);
56                 }
57                 else
58                 {
59                         $end = strtolower(substr($str, -1));
60
61                         if ($end == 's')
62                         {
63                                 $str = substr($str, 0, strlen($str)-1);
64                         }
65                 }
66
67                 return $str;
68         }
69 }
70
71 // --------------------------------------------------------------------
72
73 /**
74  * Plural
75  *
76  * Takes a singular word and makes it plural
77  *
78  * @access      public
79  * @param       string
80  * @param       bool
81  * @return      str
82  */
83 if ( ! function_exists('plural'))
84 {
85         function plural($str, $force = FALSE)
86         {   
87         $str = trim($str);
88                 $end = substr($str, -1);
89
90                 if (preg_match('/y/i',$end))
91                 {
92                         // Y preceded by vowel => regular plural
93                         $vowels = array('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
94                         $str = in_array(substr($str, -2, 1), $vowels) ? $str.'s' : substr($str, 0, -1).'ies';
95                 }
96                 elseif (preg_match('/h/i',$end))
97                 {
98             if(preg_match('/^[c|s]h$/i',substr($str, -2)))
99                         {
100                                 $str .= 'es';
101                         }
102                         else
103                         {
104                                 $str .= 's';
105                         }
106                 }
107                 elseif (preg_match('/s/i',$end))
108                 {
109                         if ($force == TRUE)
110                         {
111                                 $str .= 'es';
112                         }
113                 }
114                 else
115                 {
116                         $str .= 's';
117                 }
118
119                 return $str;
120         }
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126  * Camelize
127  *
128  * Takes multiple words separated by spaces or underscores and camelizes them
129  *
130  * @access      public
131  * @param       string
132  * @return      str
133  */
134 if ( ! function_exists('camelize'))
135 {
136         function camelize($str)
137         {
138                 $str = 'x'.strtolower(trim($str));
139                 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
140                 return substr(str_replace(' ', '', $str), 1);
141         }
142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147  * Underscore
148  *
149  * Takes multiple words separated by spaces and underscores them
150  *
151  * @access      public
152  * @param       string
153  * @return      str
154  */
155 if ( ! function_exists('underscore'))
156 {
157         function underscore($str)
158         {
159                 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
160         }
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166  * Humanize
167  *
168  * Takes multiple words separated by underscores and changes them to spaces
169  *
170  * @access      public
171  * @param       string
172  * @return      str
173  */
174 if ( ! function_exists('humanize'))
175 {
176         function humanize($str)
177         {
178                 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
179         }
180 }
181
182
183 /* End of file inflector_helper.php */
184 /* Location: ./system/helpers/inflector_helper.php */