blob: c7dd96977f0070ca2c40c1fd5425eb4e4dc9c7dd [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreeve684bda2012-03-26 13:47:29 +03008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreeve684bda2012-03-26 13:47:29 +030010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter String Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/string_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('trim_slashes'))
53{
Timothy Warrenb75faa12012-04-27 12:03:32 -040054 /**
55 * Trim Slashes
56 *
57 * Removes any leading/trailing slashes from a string:
58 *
59 * /this/that/theother/
60 *
61 * becomes:
62 *
63 * this/that/theother
64 *
Andrey Andreevf7c39d62012-11-08 22:01:33 +020065 * @todo Remove in version 3.1+.
66 * @deprecated 3.0.0 This is just an alias for PHP's native trim()
67 *
Timothy Warrenb75faa12012-04-27 12:03:32 -040068 * @param string
69 * @return string
70 */
Derek Allard2067d1a2008-11-13 22:59:24 +000071 function trim_slashes($str)
72 {
Barry Mienydd671972010-10-04 16:33:58 +020073 return trim($str, '/');
Derek Jones1e141922010-03-05 10:24:50 -060074 }
Derek Allard2067d1a2008-11-13 22:59:24 +000075}
Derek Jones1e141922010-03-05 10:24:50 -060076
Derek Allard2067d1a2008-11-13 22:59:24 +000077// ------------------------------------------------------------------------
78
Derek Allard2067d1a2008-11-13 22:59:24 +000079if ( ! function_exists('strip_slashes'))
80{
Timothy Warrenb75faa12012-04-27 12:03:32 -040081 /**
82 * Strip Slashes
83 *
84 * Removes slashes contained in a string or in an array
85 *
86 * @param mixed string or array
87 * @return mixed string or array
88 */
Derek Allard2067d1a2008-11-13 22:59:24 +000089 function strip_slashes($str)
90 {
Andrey Andreev09375d72012-01-19 14:57:46 +020091 if ( ! is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020092 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020093 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Andrey Andreev09375d72012-01-19 14:57:46 +020096 foreach ($str as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Andrey Andreev09375d72012-01-19 14:57:46 +020098 $str[$key] = strip_slashes($val);
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
100
101 return $str;
102 }
103}
104
105// ------------------------------------------------------------------------
106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107if ( ! function_exists('strip_quotes'))
108{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400109 /**
110 * Strip Quotes
111 *
112 * Removes single and double quotes from a string
113 *
114 * @param string
115 * @return string
116 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 function strip_quotes($str)
118 {
119 return str_replace(array('"', "'"), '', $str);
120 }
121}
122
123// ------------------------------------------------------------------------
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125if ( ! function_exists('quotes_to_entities'))
126{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400127 /**
128 * Quotes to Entities
129 *
130 * Converts single and double quotes to entities
131 *
132 * @param string
133 * @return string
134 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200136 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
138 }
139}
140
141// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143if ( ! function_exists('reduce_double_slashes'))
144{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400145 /**
146 * Reduce Double Slashes
147 *
148 * Converts double slashes in a string to a single slash,
149 * except those found in http://
150 *
151 * http://www.some-site.com//index.php
152 *
153 * becomes:
154 *
155 * http://www.some-site.com/index.php
156 *
157 * @param string
158 * @return string
159 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 function reduce_double_slashes($str)
161 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300162 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164}
Derek Jones1e141922010-03-05 10:24:50 -0600165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166// ------------------------------------------------------------------------
167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168if ( ! function_exists('reduce_multiples'))
169{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400170 /**
171 * Reduce Multiples
172 *
173 * Reduces multiple instances of a particular character. Example:
174 *
175 * Fred, Bill,, Joe, Jimmy
176 *
177 * becomes:
178 *
179 * Fred, Bill, Joe, Jimmy
180 *
181 * @param string
182 * @param string the character you wish to reduce
183 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
184 * @return string
185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 function reduce_multiples($str, $character = ',', $trim = FALSE)
187 {
188 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200189 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191}
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193// ------------------------------------------------------------------------
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000196{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400197 /**
Andrey Andreev618870f2017-03-01 14:09:26 +0200198 * Create a "Random" String
Timothy Warrenb75faa12012-04-27 12:03:32 -0400199 *
Olanipekun Femi08dd57c2014-02-07 08:16:48 -0800200 * @param string type of random string. basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
Timothy Warrenb75faa12012-04-27 12:03:32 -0400201 * @param int number of characters
202 * @return string
203 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200205 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200206 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Andrey Andreev1a361202012-03-26 22:08:49 +0300208 case 'basic':
209 return mt_rand();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200210 case 'alnum':
211 case 'numeric':
212 case 'nozero':
213 case 'alpha':
214 switch ($type)
215 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200216 case 'alpha':
217 $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200218 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200219 case 'alnum':
220 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200221 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200222 case 'numeric':
223 $pool = '0123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200224 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200225 case 'nozero':
226 $pool = '123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200227 break;
228 }
Andrey Andreev75124a52012-03-13 12:47:58 +0200229 return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
Andrey Andreev442682e2012-11-08 22:52:12 +0200230 case 'unique': // todo: remove in 3.1+
Andrey Andreev1a361202012-03-26 22:08:49 +0300231 case 'md5':
232 return md5(uniqid(mt_rand()));
Andrey Andreev442682e2012-11-08 22:52:12 +0200233 case 'encrypt': // todo: remove in 3.1+
Andrey Andreev4921fed2012-01-07 01:28:07 +0200234 case 'sha1':
Andrey Andreev1a361202012-03-26 22:08:49 +0300235 return sha1(uniqid(mt_rand(), TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237 }
238}
Derek Jones1e141922010-03-05 10:24:50 -0600239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240// ------------------------------------------------------------------------
241
Andrey Andreev84c3b272012-03-26 21:50:00 +0300242if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600243{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400244 /**
245 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
246 *
247 * @param string required
248 * @param string What should the duplicate number be appended with
249 * @param string Which number should be used for the first dupe increment
250 * @return string
251 */
Andrey Andreev84c3b272012-03-26 21:50:00 +0300252 function increment_string($str, $separator = '_', $first = 1)
253 {
Andrey Andreev9bb94692015-08-07 13:29:11 +0300254 preg_match('/(.+)'.preg_quote($separator, '/').'([0-9]+)$/', $str, $match);
Andrey Andreev84c3b272012-03-26 21:50:00 +0300255 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
256 }
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600257}
258
259// ------------------------------------------------------------------------
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261if ( ! function_exists('alternator'))
262{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400263 /**
264 * Alternator
265 *
266 * Allows strings to be alternated. See docs...
267 *
268 * @param string (as many parameters as needed)
269 * @return string
270 */
Andrey Andreeva18c6092015-12-03 17:53:14 +0200271 function alternator()
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Barry Mienydd671972010-10-04 16:33:58 +0200273 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
Alex Bilbie773ccc32012-06-02 11:11:08 +0100275 if (func_num_args() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 $i = 0;
278 return '';
279 }
Andrey Andreeva18c6092015-12-03 17:53:14 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 $args = func_get_args();
282 return $args[($i++ % count($args))];
283 }
284}
285
286// ------------------------------------------------------------------------
287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288if ( ! function_exists('repeater'))
289{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400290 /**
291 * Repeater function
292 *
Andrey Andreev60826db2012-10-27 14:45:23 +0300293 * @todo Remove in version 3.1+.
294 * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat()
295 *
296 * @param string $data String to repeat
297 * @param int $num Number of repeats
Timothy Warrenb75faa12012-04-27 12:03:32 -0400298 * @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}