blob: b1864cb0b79b788e0b06391510e795c15f07f9e6 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/directory_helper.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27
28
29// --------------------------------------------------------------------
30
31/**
32 * Singular
33 *
Derek Joneseea2bda2007-07-11 22:09:45 +000034 * Takes a plural word and makes it singular
Derek Allardd2df9bc2007-04-15 17:41:17 +000035 *
36 * @access public
37 * @param string
38 * @return str
39 */
40function singular($str)
41{
Derek Allardf5a51982007-07-11 19:35:09 +000042 $str = strtolower(trim($str));
43 $end = substr($str, -3);
44
45 if ($end == 'ies')
46 {
47 $str = substr($str, 0, strlen($str)-3).'y';
48 }
49 elseif ($end == 'ses')
50 {
51 $str = substr($str, 0, strlen($str)-2);
52 }
53 else
54 {
55 $end = substr($str, -1);
56
57 if ($end == 's')
58 {
59 $str = substr($str, 0, strlen($str)-1);
60 }
61 }
62
63 return $str;
Derek Allardd2df9bc2007-04-15 17:41:17 +000064}
65
Derek Allardf5a51982007-07-11 19:35:09 +000066
Derek Allardd2df9bc2007-04-15 17:41:17 +000067// --------------------------------------------------------------------
68
69/**
70 * Plural
71 *
Derek Joneseea2bda2007-07-11 22:09:45 +000072 * Takes a singular word and makes it plural
Derek Allardd2df9bc2007-04-15 17:41:17 +000073 *
74 * @access public
75 * @param string
Derek Allardf5a51982007-07-11 19:35:09 +000076 * @param bool
Derek Allardd2df9bc2007-04-15 17:41:17 +000077 * @return str
78 */
Derek Allardf5a51982007-07-11 19:35:09 +000079function plural($str, $force = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +000080{
Derek Allardf5a51982007-07-11 19:35:09 +000081 $str = strtolower(trim($str));
82 $end = substr($str, -1);
Derek Allardd2df9bc2007-04-15 17:41:17 +000083
Derek Allardf5a51982007-07-11 19:35:09 +000084 if ($end == 'y')
85 {
86 $str = substr($str, 0, strlen($str)-1).'ies';
87 }
88 elseif ($end == 's')
89 {
90 if ($force == TRUE)
91 {
92 $str .= 'es';
93 }
94 }
95 else
96 {
97 $str .= 's';
98 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000099
Derek Allardf5a51982007-07-11 19:35:09 +0000100 return $str;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000101}
102
Derek Allardf5a51982007-07-11 19:35:09 +0000103
Derek Allardd2df9bc2007-04-15 17:41:17 +0000104// --------------------------------------------------------------------
105
106/**
107 * Camelize
108 *
109 * Takes multiple words separated by spaces or underscores and camelizes them
110 *
111 * @access public
112 * @param string
113 * @return str
114 */
115function camelize($str)
116{
117 $str = 'x'.strtolower(trim($str));
118 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
119 return substr(str_replace(' ', '', $str), 1);
120}
121
122// --------------------------------------------------------------------
123
124/**
125 * Underscore
126 *
127 * Takes multiple words separated by spaces and underscores them
128 *
129 * @access public
130 * @param string
131 * @return str
132 */
133function underscore($str)
134{
135 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
136}
137
138// --------------------------------------------------------------------
139
140/**
141 * Humanize
142 *
143 * Takes multiple words separated by underscores and changes them to spaces
144 *
145 * @access public
146 * @param string
147 * @return str
148 */
149function humanize($str)
150{
151 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
152}
153
admin5b179b52006-09-20 23:44:53 +0000154?>