blob: aed35c15731626ddf171288f386cfc1b3f38d721 [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
40/**
41 * Trim Slashes
42 *
Derek Jones1e141922010-03-05 10:24:50 -060043 * Removes any leading/trailing slashes from a string:
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * /this/that/theother/
46 *
47 * becomes:
48 *
49 * this/that/theother
50 *
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @param string
52 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020053 */
Derek Allard2067d1a2008-11-13 22:59:24 +000054if ( ! function_exists('trim_slashes'))
55{
56 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
64/**
65 * Strip Slashes
66 *
67 * Removes slashes contained in a string or in an array
68 *
Derek Allard2067d1a2008-11-13 22:59:24 +000069 * @param mixed string or array
70 * @return mixed string or array
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('strip_slashes'))
73{
74 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
94/**
95 * Strip Quotes
96 *
97 * Removes single and double quotes from a string
98 *
Derek Allard2067d1a2008-11-13 22:59:24 +000099 * @param string
100 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200101 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000102if ( ! function_exists('strip_quotes'))
103{
104 function strip_quotes($str)
105 {
106 return str_replace(array('"', "'"), '', $str);
107 }
108}
109
110// ------------------------------------------------------------------------
111
112/**
113 * Quotes to Entities
114 *
115 * Converts single and double quotes to entities
116 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * @param string
118 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200119 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000120if ( ! function_exists('quotes_to_entities'))
121{
122 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 +0000130/**
131 * Reduce Double Slashes
132 *
133 * Converts double slashes in a string to a single slash,
134 * except those found in http://
135 *
136 * http://www.some-site.com//index.php
137 *
138 * becomes:
139 *
140 * http://www.some-site.com/index.php
141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string
143 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200144 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000145if ( ! function_exists('reduce_double_slashes'))
146{
147 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
155/**
156 * Reduce Multiples
157 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500158 * Reduces multiple instances of a particular character. Example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 *
160 * Fred, Bill,, Joe, Jimmy
161 *
162 * becomes:
163 *
164 * Fred, Bill, Joe, Jimmy
165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string
167 * @param string the character you wish to reduce
168 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
169 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200170 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000171if ( ! function_exists('reduce_multiples'))
172{
173 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
188/**
189 * Create a Random String
190 *
191 * Useful for generating passwords or hashes.
192 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500193 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
Andrey Andreev84c3b272012-03-26 21:50:00 +0300194 * @param int number of characters
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200196 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000197if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000198{
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
245/**
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600246 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
247 *
Andrey Andreev84c3b272012-03-26 21:50:00 +0300248 * @param string required
249 * @param string What should the duplicate number be appended with
250 * @param string Which number should be used for the first dupe increment
251 * @return string
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600252 */
Andrey Andreev84c3b272012-03-26 21:50:00 +0300253if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600254{
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
265/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * Alternator
267 *
Andrey Andreev84c3b272012-03-26 21:50:00 +0300268 * Allows strings to be alternated. See docs...
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param string (as many parameters as needed)
271 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200272 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000273if ( ! function_exists('alternator'))
274{
275 function alternator()
276 {
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
291/**
292 * Repeater function
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string
Andrey Andreev84c3b272012-03-26 21:50:00 +0300295 * @param int number of repeats
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200297 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000298if ( ! function_exists('repeater'))
299{
300 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 */