Take two:
[www-register-wizard.git] / helpers / string_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 String 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/string_helper.html\r
26  */\r
27 \r
28 // ------------------------------------------------------------------------\r
29 \r
30 /**\r
31  * Trim Slashes\r
32  *\r
33  * Removes any leading/traling slashes from a string:\r
34  *\r
35  * /this/that/theother/\r
36  *\r
37  * becomes:\r
38  *\r
39  * this/that/theother\r
40  *\r
41  * @access      public\r
42  * @param       string\r
43  * @return      string\r
44  */     \r
45 if ( ! function_exists('trim_slashes'))\r
46 {\r
47         function trim_slashes($str)\r
48         {\r
49                 return trim($str, '/');\r
50         } \r
51 }\r
52         \r
53 // ------------------------------------------------------------------------\r
54 \r
55 /**\r
56  * Strip Slashes\r
57  *\r
58  * Removes slashes contained in a string or in an array\r
59  *\r
60  * @access      public\r
61  * @param       mixed   string or array\r
62  * @return      mixed   string or array\r
63  */     \r
64 if ( ! function_exists('strip_slashes'))\r
65 {\r
66         function strip_slashes($str)\r
67         {\r
68                 if (is_array($str))\r
69                 {       \r
70                         foreach ($str as $key => $val)\r
71                         {\r
72                                 $str[$key] = strip_slashes($val);\r
73                         }\r
74                 }\r
75                 else\r
76                 {\r
77                         $str = stripslashes($str);\r
78                 }\r
79         \r
80                 return $str;\r
81         }\r
82 }\r
83 \r
84 // ------------------------------------------------------------------------\r
85 \r
86 /**\r
87  * Strip Quotes\r
88  *\r
89  * Removes single and double quotes from a string\r
90  *\r
91  * @access      public\r
92  * @param       string\r
93  * @return      string\r
94  */     \r
95 if ( ! function_exists('strip_quotes'))\r
96 {\r
97         function strip_quotes($str)\r
98         {\r
99                 return str_replace(array('"', "'"), '', $str);\r
100         }\r
101 }\r
102 \r
103 // ------------------------------------------------------------------------\r
104 \r
105 /**\r
106  * Quotes to Entities\r
107  *\r
108  * Converts single and double quotes to entities\r
109  *\r
110  * @access      public\r
111  * @param       string\r
112  * @return      string\r
113  */     \r
114 if ( ! function_exists('quotes_to_entities'))\r
115 {\r
116         function quotes_to_entities($str)\r
117         {       \r
118                 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);\r
119         }\r
120 }\r
121 \r
122 // ------------------------------------------------------------------------\r
123 /**\r
124  * Reduce Double Slashes\r
125  *\r
126  * Converts double slashes in a string to a single slash,\r
127  * except those found in http://\r
128  *\r
129  * http://www.some-site.com//index.php\r
130  *\r
131  * becomes:\r
132  *\r
133  * http://www.some-site.com/index.php\r
134  *\r
135  * @access      public\r
136  * @param       string\r
137  * @return      string\r
138  */     \r
139 if ( ! function_exists('reduce_double_slashes'))\r
140 {\r
141         function reduce_double_slashes($str)\r
142         {\r
143                 return preg_replace("#([^:])//+#", "\\1/", $str);\r
144         }\r
145 }\r
146         \r
147 // ------------------------------------------------------------------------\r
148 \r
149 /**\r
150  * Reduce Multiples\r
151  *\r
152  * Reduces multiple instances of a particular character.  Example:\r
153  *\r
154  * Fred, Bill,, Joe, Jimmy\r
155  *\r
156  * becomes:\r
157  *\r
158  * Fred, Bill, Joe, Jimmy\r
159  *\r
160  * @access      public\r
161  * @param       string\r
162  * @param       string  the character you wish to reduce\r
163  * @param       bool    TRUE/FALSE - whether to trim the character from the beginning/end\r
164  * @return      string\r
165  */     \r
166 if ( ! function_exists('reduce_multiples'))\r
167 {\r
168         function reduce_multiples($str, $character = ',', $trim = FALSE)\r
169         {\r
170                 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);\r
171 \r
172                 if ($trim === TRUE)\r
173                 {\r
174                         $str = trim($str, $character);\r
175                 }\r
176 \r
177                 return $str;\r
178         }\r
179 }\r
180         \r
181 // ------------------------------------------------------------------------\r
182 \r
183 /**\r
184  * Create a Random String\r
185  *\r
186  * Useful for generating passwords or hashes.\r
187  *\r
188  * @access      public\r
189  * @param       string  type of random string.  Options: alunum, numeric, nozero, unique\r
190  * @param       integer number of characters\r
191  * @return      string\r
192  */\r
193 if ( ! function_exists('random_string'))\r
194 {       \r
195         function random_string($type = 'alnum', $len = 8)\r
196         {                                       \r
197                 switch($type)\r
198                 {\r
199                         case 'alnum'    :\r
200                         case 'numeric'  :\r
201                         case 'nozero'   :\r
202                 \r
203                                         switch ($type)\r
204                                         {\r
205                                                 case 'alnum'    :       $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';\r
206                                                         break;\r
207                                                 case 'numeric'  :       $pool = '0123456789';\r
208                                                         break;\r
209                                                 case 'nozero'   :       $pool = '123456789';\r
210                                                         break;\r
211                                         }\r
212 \r
213                                         $str = '';\r
214                                         for ($i=0; $i < $len; $i++)\r
215                                         {\r
216                                                 $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);\r
217                                         }\r
218                                         return $str;\r
219                           break;\r
220                         case 'unique' : return md5(uniqid(mt_rand()));\r
221                           break;\r
222                 }\r
223         }\r
224 }\r
225 \r
226 // ------------------------------------------------------------------------\r
227 \r
228 /**\r
229  * Alternator\r
230  *\r
231  * Allows strings to be alternated.  See docs...\r
232  *\r
233  * @access      public\r
234  * @param       string (as many parameters as needed)\r
235  * @return      string\r
236  */     \r
237 if ( ! function_exists('alternator'))\r
238 {\r
239         function alternator()\r
240         {\r
241                 static $i;      \r
242 \r
243                 if (func_num_args() == 0)\r
244                 {\r
245                         $i = 0;\r
246                         return '';\r
247                 }\r
248                 $args = func_get_args();\r
249                 return $args[($i++ % count($args))];\r
250         }\r
251 }\r
252 \r
253 // ------------------------------------------------------------------------\r
254 \r
255 /**\r
256  * Repeater function\r
257  *\r
258  * @access      public\r
259  * @param       string\r
260  * @param       integer number of repeats\r
261  * @return      string\r
262  */     \r
263 if ( ! function_exists('repeater'))\r
264 {\r
265         function repeater($data, $num = 1)\r
266         {\r
267                 return (($num > 0) ? str_repeat($data, $num) : '');\r
268         } \r
269 }\r
270 \r
271 \r
272 /* End of file string_helper.php */\r
273 /* Location: ./system/helpers/string_helper.php */