blob: b42799a8e2b14f9c3247e8259d0fe8faf1c86341 [file] [log] [blame]
Andrey Andreeve684bda2012-03-26 13:47:29 +03001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreeve684bda2012-03-26 13:47:29 +03008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve684bda2012-03-26 13:47:29 +030010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter String Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/string_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('trim_slashes'))
41{
Timothy Warrenb75faa12012-04-27 12:03:32 -040042 /**
43 * Trim Slashes
44 *
45 * Removes any leading/trailing slashes from a string:
46 *
47 * /this/that/theother/
48 *
49 * becomes:
50 *
51 * this/that/theother
52 *
53 * @param string
54 * @return string
55 */
Derek Allard2067d1a2008-11-13 22:59:24 +000056 function trim_slashes($str)
57 {
Barry Mienydd671972010-10-04 16:33:58 +020058 return trim($str, '/');
Derek Jones1e141922010-03-05 10:24:50 -060059 }
Derek Allard2067d1a2008-11-13 22:59:24 +000060}
Derek Jones1e141922010-03-05 10:24:50 -060061
Derek Allard2067d1a2008-11-13 22:59:24 +000062// ------------------------------------------------------------------------
63
Derek Allard2067d1a2008-11-13 22:59:24 +000064if ( ! function_exists('strip_slashes'))
65{
Timothy Warrenb75faa12012-04-27 12:03:32 -040066 /**
67 * Strip Slashes
68 *
69 * Removes slashes contained in a string or in an array
70 *
71 * @param mixed string or array
72 * @return mixed string or array
73 */
Derek Allard2067d1a2008-11-13 22:59:24 +000074 function strip_slashes($str)
75 {
76 if (is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020077 {
Derek Allard2067d1a2008-11-13 22:59:24 +000078 foreach ($str as $key => $val)
79 {
80 $str[$key] = strip_slashes($val);
81 }
82 }
83 else
84 {
85 $str = stripslashes($str);
86 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 return $str;
89 }
90}
91
92// ------------------------------------------------------------------------
93
Derek Allard2067d1a2008-11-13 22:59:24 +000094if ( ! function_exists('strip_quotes'))
95{
Timothy Warrenb75faa12012-04-27 12:03:32 -040096 /**
97 * Strip Quotes
98 *
99 * Removes single and double quotes from a string
100 *
101 * @param string
102 * @return string
103 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 function strip_quotes($str)
105 {
106 return str_replace(array('"', "'"), '', $str);
107 }
108}
109
110// ------------------------------------------------------------------------
111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112if ( ! function_exists('quotes_to_entities'))
113{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400114 /**
115 * Quotes to Entities
116 *
117 * Converts single and double quotes to entities
118 *
119 * @param string
120 * @return string
121 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200123 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
125 }
126}
127
128// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130if ( ! function_exists('reduce_double_slashes'))
131{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400132 /**
133 * Reduce Double Slashes
134 *
135 * Converts double slashes in a string to a single slash,
136 * except those found in http://
137 *
138 * http://www.some-site.com//index.php
139 *
140 * becomes:
141 *
142 * http://www.some-site.com/index.php
143 *
144 * @param string
145 * @return string
146 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 function reduce_double_slashes($str)
148 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300149 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 }
151}
Derek Jones1e141922010-03-05 10:24:50 -0600152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153// ------------------------------------------------------------------------
154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155if ( ! function_exists('reduce_multiples'))
156{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400157 /**
158 * Reduce Multiples
159 *
160 * Reduces multiple instances of a particular character. Example:
161 *
162 * Fred, Bill,, Joe, Jimmy
163 *
164 * becomes:
165 *
166 * Fred, Bill, Joe, Jimmy
167 *
168 * @param string
169 * @param string the character you wish to reduce
170 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
171 * @return string
172 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 function reduce_multiples($str, $character = ',', $trim = FALSE)
174 {
175 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
176
177 if ($trim === TRUE)
178 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300179 return trim($str, $character);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 return $str;
183 }
184}
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186// ------------------------------------------------------------------------
187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000189{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400190 /**
191 * Create a Random String
192 *
193 * Useful for generating passwords or hashes.
194 *
195 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
196 * @param int number of characters
197 * @return string
198 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200200 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 switch($type)
202 {
Derek Allard472dd212010-01-23 20:03:27 +0000203 case 'basic' : return mt_rand();
Barry Mienydd671972010-10-04 16:33:58 +0200204 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 case 'alnum' :
206 case 'numeric' :
207 case 'nozero' :
Derek Allard472dd212010-01-23 20:03:27 +0000208 case 'alpha' :
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 switch ($type)
211 {
Derek Allard472dd212010-01-23 20:03:27 +0000212 case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
213 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
215 break;
216 case 'numeric' : $pool = '0123456789';
217 break;
218 case 'nozero' : $pool = '123456789';
219 break;
220 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300221
druu9e2df7c2011-09-06 22:19:59 +0300222 $str = substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len);
Andrey Andreeve684bda2012-03-26 13:47:29 +0300223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return $str;
Barry Mienydd671972010-10-04 16:33:58 +0200225 break;
226 case 'unique' :
227 case 'md5' :
228
Derek Allard472dd212010-01-23 20:03:27 +0000229 return md5(uniqid(mt_rand()));
Barry Mienydd671972010-10-04 16:33:58 +0200230 break;
231 case 'encrypt' :
232 case 'sha1' :
233
Derek Allard472dd212010-01-23 20:03:27 +0000234 $CI =& get_instance();
235 $CI->load->helper('security');
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard472dd212010-01-23 20:03:27 +0000237 return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
Barry Mienydd671972010-10-04 16:33:58 +0200238 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240 }
241}
Derek Jones1e141922010-03-05 10:24:50 -0600242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243// ------------------------------------------------------------------------
244
Andrey Andreev84c3b272012-03-26 21:50:00 +0300245if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600246{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400247 /**
248 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
249 *
250 * @param string required
251 * @param string What should the duplicate number be appended with
252 * @param string Which number should be used for the first dupe increment
253 * @return string
254 */
Andrey Andreev84c3b272012-03-26 21:50:00 +0300255 function increment_string($str, $separator = '_', $first = 1)
256 {
257 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600258
Andrey Andreev84c3b272012-03-26 21:50:00 +0300259 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
260 }
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600261}
262
263// ------------------------------------------------------------------------
264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265if ( ! function_exists('alternator'))
266{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400267 /**
268 * Alternator
269 *
270 * Allows strings to be alternated. See docs...
271 *
272 * @param string (as many parameters as needed)
273 * @return string
274 */
275 function alternator($args)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Barry Mienydd671972010-10-04 16:33:58 +0200277 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
279 if (func_num_args() == 0)
280 {
281 $i = 0;
282 return '';
283 }
284 $args = func_get_args();
285 return $args[($i++ % count($args))];
286 }
287}
288
289// ------------------------------------------------------------------------
290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291if ( ! function_exists('repeater'))
292{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400293 /**
294 * Repeater function
295 *
296 * @param string
297 * @param int number of repeats
298 * @return string
299 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 function repeater($data, $num = 1)
301 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300302 return ($num > 0) ? str_repeat($data, $num) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200303 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000304}
305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306/* End of file string_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000307/* Location: ./system/helpers/string_helper.php */