blob: 666ec40b8e7173ca39d8435f3bae61626bf47416 [file] [log] [blame]
admin5b179b52006-09-20 23:44:53 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
admin5b179b52006-09-20 23:44:53 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
admin5b179b52006-09-20 23:44:53 +000016// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Inflector Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @link http://www.codeigniter.com/user_guide/helpers/directory_helper.html
26 */
27
28
29// --------------------------------------------------------------------
30
31/**
32 * Singular
33 *
admin592cdcb2006-09-22 18:45:42 +000034 * Takes a singular word and makes it plural
admin5b179b52006-09-20 23:44:53 +000035 *
36 * @access public
37 * @param string
38 * @return str
39 */
40function singular($str)
41{
42 $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 else
50 {
51 $end = substr($str, -1);
52
53 if ($end == 's')
54 {
55 $str = substr($str, 0, strlen($str)-1);
56 }
57 }
58
59 return $str;
60}
61
62// --------------------------------------------------------------------
63
64/**
65 * Plural
66 *
admin592cdcb2006-09-22 18:45:42 +000067 * Takes a plural word and makes it singular
admin5b179b52006-09-20 23:44:53 +000068 *
69 * @access public
70 * @param string
71 * @return str
72 */
73function plural($str)
74{
75 $str = strtolower(trim($str));
76 $end = substr($str, -1);
77
78 if ($end == 'y')
79 {
80 $str = substr($str, 0, strlen($str)-1).'ies';
81 }
82 elseif ($end != 's')
83 {
84 $str .= 's';
85 }
86
87 return $str;
88}
89
90// --------------------------------------------------------------------
91
92/**
93 * Camelize
94 *
95 * Takes multiple words separated by spaces or underscores and camelizes them
96 *
97 * @access public
98 * @param string
99 * @return str
100 */
101function camelize($str)
102{
103 $str = 'x'.strtolower(trim($str));
104 $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
105 return substr(str_replace(' ', '', $str), 1);
106}
107
108// --------------------------------------------------------------------
109
110/**
111 * Underscore
112 *
113 * Takes multiple words separated by spaces and underscores them
114 *
115 * @access public
116 * @param string
117 * @return str
118 */
119function underscore($str)
120{
121 return preg_replace('/[\s]+/', '_', strtolower(trim($str)));
122}
123
124// --------------------------------------------------------------------
125
126/**
127 * Humanize
128 *
129 * Takes multiple words separated by underscores and changes them to spaces
130 *
131 * @access public
132 * @param string
133 * @return str
134 */
135function humanize($str)
136{
137 return ucwords(preg_replace('/[_]+/', ' ', strtolower(trim($str))));
138}
139
140?>