1<?php
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP
6 *
7 * This content is released under the MIT License (MIT)
8 *
9 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
10 *
11 * 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:
17 *
18 * 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
31 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32 * @copyright	Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license	http://opensource.org/licenses/MIT	MIT License
34 * @link	https://codeigniter.com
35 * @since	Version 1.0.0
36 * @filesource
37 */
38defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * CodeIgniter String Helpers
42 *
43 * @package		CodeIgniter
44 * @subpackage	Helpers
45 * @category	Helpers
46 * @author		EllisLab Dev Team
47 * @link		https://codeigniter.com/user_guide/helpers/string_helper.html
48 */
49
50// ------------------------------------------------------------------------
51
52if ( ! function_exists('trim_slashes'))
53{
54	/**
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	 *
65	 * @todo	Remove in version 3.1+.
66	 * @deprecated	3.0.0	This is just an alias for PHP's native trim()
67	 *
68	 * @param	string
69	 * @return	string
70	 */
71	function trim_slashes($str)
72	{
73		return trim($str, '/');
74	}
75}
76
77// ------------------------------------------------------------------------
78
79if ( ! function_exists('strip_slashes'))
80{
81	/**
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	 */
89	function strip_slashes($str)
90	{
91		if ( ! is_array($str))
92		{
93			return stripslashes($str);
94		}
95
96		foreach ($str as $key => $val)
97		{
98			$str[$key] = strip_slashes($val);
99		}
100
101		return $str;
102	}
103}
104
105// ------------------------------------------------------------------------
106
107if ( ! function_exists('strip_quotes'))
108{
109	/**
110	 * Strip Quotes
111	 *
112	 * Removes single and double quotes from a string
113	 *
114	 * @param	string
115	 * @return	string
116	 */
117	function strip_quotes($str)
118	{
119		return str_replace(array('"', "'"), '', $str);
120	}
121}
122
123// ------------------------------------------------------------------------
124
125if ( ! function_exists('quotes_to_entities'))
126{
127	/**
128	 * Quotes to Entities
129	 *
130	 * Converts single and double quotes to entities
131	 *
132	 * @param	string
133	 * @return	string
134	 */
135	function quotes_to_entities($str)
136	{
137		return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
138	}
139}
140
141// ------------------------------------------------------------------------
142
143if ( ! function_exists('reduce_double_slashes'))
144{
145	/**
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	 */
160	function reduce_double_slashes($str)
161	{
162		return preg_replace('#(^|[^:])//+#', '\\1/', $str);
163	}
164}
165
166// ------------------------------------------------------------------------
167
168if ( ! function_exists('reduce_multiples'))
169{
170	/**
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	 */
186	function reduce_multiples($str, $character = ',', $trim = FALSE)
187	{
188		$str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
189		return ($trim === TRUE) ? trim($str, $character) : $str;
190	}
191}
192
193// ------------------------------------------------------------------------
194
195if ( ! function_exists('random_string'))
196{
197	/**
198	 * Create a "Random" String
199	 *
200	 * @param	string	type of random string.  basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
201	 * @param	int	number of characters
202	 * @return	string
203	 */
204	function random_string($type = 'alnum', $len = 8)
205	{
206		switch ($type)
207		{
208			case 'basic':
209				return mt_rand();
210			case 'alnum':
211			case 'numeric':
212			case 'nozero':
213			case 'alpha':
214				switch ($type)
215				{
216					case 'alpha':
217						$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
218						break;
219					case 'alnum':
220						$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
221						break;
222					case 'numeric':
223						$pool = '0123456789';
224						break;
225					case 'nozero':
226						$pool = '123456789';
227						break;
228				}
229				return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
230			case 'unique': // todo: remove in 3.1+
231			case 'md5':
232				return md5(uniqid(mt_rand()));
233			case 'encrypt': // todo: remove in 3.1+
234			case 'sha1':
235				return sha1(uniqid(mt_rand(), TRUE));
236		}
237	}
238}
239
240// ------------------------------------------------------------------------
241
242if ( ! function_exists('increment_string'))
243{
244	/**
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	 */
252	function increment_string($str, $separator = '_', $first = 1)
253	{
254		preg_match('/(.+)'.preg_quote($separator, '/').'([0-9]+)$/', $str, $match);
255		return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
256	}
257}
258
259// ------------------------------------------------------------------------
260
261if ( ! function_exists('alternator'))
262{
263	/**
264	 * Alternator
265	 *
266	 * Allows strings to be alternated. See docs...
267	 *
268	 * @param	string (as many parameters as needed)
269	 * @return	string
270	 */
271	function alternator()
272	{
273		static $i;
274
275		if (func_num_args() === 0)
276		{
277			$i = 0;
278			return '';
279		}
280
281		$args = func_get_args();
282		return $args[($i++ % count($args))];
283	}
284}
285
286// ------------------------------------------------------------------------
287
288if ( ! function_exists('repeater'))
289{
290	/**
291	 * Repeater function
292	 *
293	 * @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
298	 * @return	string
299	 */
300	function repeater($data, $num = 1)
301	{
302		return ($num > 0) ? str_repeat($data, $num) : '';
303	}
304}
305