Subversion-Projekte lars-tiefland.ci

Revision

Revision 68 | Revision 2107 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 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
 *
2049 lars 9
 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
68 lars 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/)
2049 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('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
 
52
if ( ! 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
 
79
if ( ! 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
 
107
if ( ! 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
 
125
if ( ! 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
 
143
if ( ! 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
 
168
if ( ! 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
 
195
if ( ! function_exists('random_string'))
196
{
197
	/**
198
	 * Create a Random String
199
	 *
200
	 * Useful for generating passwords or hashes.
201
	 *
202
	 * @param	string	type of random string.  basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
203
	 * @param	int	number of characters
204
	 * @return	string
205
	 */
206
	function random_string($type = 'alnum', $len = 8)
207
	{
208
		switch ($type)
209
		{
210
			case 'basic':
211
				return mt_rand();
212
			case 'alnum':
213
			case 'numeric':
214
			case 'nozero':
215
			case 'alpha':
216
				switch ($type)
217
				{
218
					case 'alpha':
219
						$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
220
						break;
221
					case 'alnum':
222
						$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
223
						break;
224
					case 'numeric':
225
						$pool = '0123456789';
226
						break;
227
					case 'nozero':
228
						$pool = '123456789';
229
						break;
230
				}
231
				return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
232
			case 'unique': // todo: remove in 3.1+
233
			case 'md5':
234
				return md5(uniqid(mt_rand()));
235
			case 'encrypt': // todo: remove in 3.1+
236
			case 'sha1':
237
				return sha1(uniqid(mt_rand(), TRUE));
238
		}
239
	}
240
}
241
 
242
// ------------------------------------------------------------------------
243
 
244
if ( ! function_exists('increment_string'))
245
{
246
	/**
247
	 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
248
	 *
249
	 * @param	string	required
250
	 * @param	string	What should the duplicate number be appended with
251
	 * @param	string	Which number should be used for the first dupe increment
252
	 * @return	string
253
	 */
254
	function increment_string($str, $separator = '_', $first = 1)
255
	{
256
		preg_match('/(.+)'.preg_quote($separator, '/').'([0-9]+)$/', $str, $match);
257
		return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
258
	}
259
}
260
 
261
// ------------------------------------------------------------------------
262
 
263
if ( ! function_exists('alternator'))
264
{
265
	/**
266
	 * Alternator
267
	 *
268
	 * Allows strings to be alternated. See docs...
269
	 *
270
	 * @param	string (as many parameters as needed)
271
	 * @return	string
272
	 */
273
	function alternator()
274
	{
275
		static $i;
276
 
277
		if (func_num_args() === 0)
278
		{
279
			$i = 0;
280
			return '';
281
		}
282
 
283
		$args = func_get_args();
284
		return $args[($i++ % count($args))];
285
	}
286
}
287
 
288
// ------------------------------------------------------------------------
289
 
290
if ( ! function_exists('repeater'))
291
{
292
	/**
293
	 * Repeater function
294
	 *
295
	 * @todo	Remove in version 3.1+.
296
	 * @deprecated	3.0.0	This is just an alias for PHP's native str_repeat()
297
	 *
298
	 * @param	string	$data	String to repeat
299
	 * @param	int	$num	Number of repeats
300
	 * @return	string
301
	 */
302
	function repeater($data, $num = 1)
303
	{
304
		return ($num > 0) ? str_repeat($data, $num) : '';
305
	}
306
}