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