blob: 4eee2a26248c70cb2df3fa0e086963d9d999c5c2 [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 {
Andrey Andreev09375d72012-01-19 14:57:46 +020076 if ( ! is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020077 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020078 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000079 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Andrey Andreev09375d72012-01-19 14:57:46 +020081 foreach ($str as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Andrey Andreev09375d72012-01-19 14:57:46 +020083 $str[$key] = strip_slashes($val);
Derek Allard2067d1a2008-11-13 22:59:24 +000084 }
85
86 return $str;
87 }
88}
89
90// ------------------------------------------------------------------------
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092if ( ! function_exists('strip_quotes'))
93{
Timothy Warrenb75faa12012-04-27 12:03:32 -040094 /**
95 * Strip Quotes
96 *
97 * Removes single and double quotes from a string
98 *
99 * @param string
100 * @return string
101 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 function strip_quotes($str)
103 {
104 return str_replace(array('"', "'"), '', $str);
105 }
106}
107
108// ------------------------------------------------------------------------
109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110if ( ! function_exists('quotes_to_entities'))
111{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400112 /**
113 * Quotes to Entities
114 *
115 * Converts single and double quotes to entities
116 *
117 * @param string
118 * @return string
119 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200121 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
123 }
124}
125
126// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128if ( ! function_exists('reduce_double_slashes'))
129{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400130 /**
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 *
142 * @param string
143 * @return string
144 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 function reduce_double_slashes($str)
146 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300147 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149}
Derek Jones1e141922010-03-05 10:24:50 -0600150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151// ------------------------------------------------------------------------
152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153if ( ! function_exists('reduce_multiples'))
154{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400155 /**
156 * Reduce Multiples
157 *
158 * Reduces multiple instances of a particular character. Example:
159 *
160 * Fred, Bill,, Joe, Jimmy
161 *
162 * becomes:
163 *
164 * Fred, Bill, Joe, Jimmy
165 *
166 * @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
170 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 function reduce_multiples($str, $character = ',', $trim = FALSE)
172 {
173 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200174 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176}
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178// ------------------------------------------------------------------------
179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000181{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400182 /**
183 * Create a Random String
184 *
185 * Useful for generating passwords or hashes.
186 *
187 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
188 * @param int number of characters
189 * @return string
190 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200193 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Andrey Andreev1a361202012-03-26 22:08:49 +0300195 case 'basic':
196 return mt_rand();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200197 case 'alnum':
198 case 'numeric':
199 case 'nozero':
200 case 'alpha':
201 switch ($type)
202 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200203 case 'alpha':
204 $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200205 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200206 case 'alnum':
207 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200208 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200209 case 'numeric':
210 $pool = '0123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200211 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200212 case 'nozero':
213 $pool = '123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200214 break;
215 }
Andrey Andreev75124a52012-03-13 12:47:58 +0200216 return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200217 case 'unique':
Andrey Andreev1a361202012-03-26 22:08:49 +0300218 case 'md5':
219 return md5(uniqid(mt_rand()));
Andrey Andreev4921fed2012-01-07 01:28:07 +0200220 case 'encrypt':
221 case 'sha1':
Andrey Andreev1a361202012-03-26 22:08:49 +0300222 return sha1(uniqid(mt_rand(), TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224 }
225}
Derek Jones1e141922010-03-05 10:24:50 -0600226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227// ------------------------------------------------------------------------
228
Andrey Andreev84c3b272012-03-26 21:50:00 +0300229if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600230{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400231 /**
232 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
233 *
234 * @param string required
235 * @param string What should the duplicate number be appended with
236 * @param string Which number should be used for the first dupe increment
237 * @return string
238 */
Andrey Andreev84c3b272012-03-26 21:50:00 +0300239 function increment_string($str, $separator = '_', $first = 1)
240 {
241 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Andrey Andreev84c3b272012-03-26 21:50:00 +0300242 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
243 }
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600244}
245
246// ------------------------------------------------------------------------
247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248if ( ! function_exists('alternator'))
249{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400250 /**
251 * Alternator
252 *
253 * Allows strings to be alternated. See docs...
254 *
255 * @param string (as many parameters as needed)
256 * @return string
257 */
258 function alternator($args)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Barry Mienydd671972010-10-04 16:33:58 +0200260 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
Alex Bilbie773ccc32012-06-02 11:11:08 +0100262 if (func_num_args() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 $i = 0;
265 return '';
266 }
267 $args = func_get_args();
268 return $args[($i++ % count($args))];
269 }
270}
271
272// ------------------------------------------------------------------------
273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274if ( ! function_exists('repeater'))
275{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400276 /**
277 * Repeater function
278 *
279 * @param string
280 * @param int number of repeats
281 * @return string
282 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 function repeater($data, $num = 1)
284 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300285 return ($num > 0) ? str_repeat($data, $num) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200286 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000287}
288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289/* End of file string_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000290/* Location: ./system/helpers/string_helper.php */