blob: d0948800b7eb89071ba90e254c1baa45ca964c3d [file] [log] [blame]
Andrey Andreev4921fed2012-01-07 01:28:07 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4921fed2012-01-07 01:28:07 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4921fed2012-01-07 01:28:07 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreev09375d72012-01-19 14:57:46 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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 *
51 * @access public
52 * @param string
53 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Derek Allard2067d1a2008-11-13 22:59:24 +000055if ( ! function_exists('trim_slashes'))
56{
57 function trim_slashes($str)
58 {
Barry Mienydd671972010-10-04 16:33:58 +020059 return trim($str, '/');
Derek Jones1e141922010-03-05 10:24:50 -060060 }
Derek Allard2067d1a2008-11-13 22:59:24 +000061}
Derek Jones1e141922010-03-05 10:24:50 -060062
Derek Allard2067d1a2008-11-13 22:59:24 +000063// ------------------------------------------------------------------------
64
65/**
66 * Strip Slashes
67 *
68 * Removes slashes contained in a string or in an array
69 *
70 * @access public
71 * @param mixed string or array
72 * @return mixed string or array
Barry Mienydd671972010-10-04 16:33:58 +020073 */
Derek Allard2067d1a2008-11-13 22:59:24 +000074if ( ! function_exists('strip_slashes'))
75{
76 function strip_slashes($str)
77 {
Andrey Andreev09375d72012-01-19 14:57:46 +020078 if ( ! is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020080 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Andrey Andreev09375d72012-01-19 14:57:46 +020083 foreach ($str as $key => $val)
84 {
85 $str[$key] = strip_slashes($val);
86 }
87
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 *
99 * @access public
100 * @param string
101 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000103if ( ! function_exists('strip_quotes'))
104{
105 function strip_quotes($str)
106 {
107 return str_replace(array('"', "'"), '', $str);
108 }
109}
110
111// ------------------------------------------------------------------------
112
113/**
114 * Quotes to Entities
115 *
116 * Converts single and double quotes to entities
117 *
118 * @access public
119 * @param string
120 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200121 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000122if ( ! function_exists('quotes_to_entities'))
123{
124 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200125 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
127 }
128}
129
130// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132/**
133 * Reduce Double Slashes
134 *
135 * Converts double slashes in a string to a single slash,
136 * except those found in http://
137 *
138 * http://www.some-site.com//index.php
139 *
140 * becomes:
141 *
142 * http://www.some-site.com/index.php
143 *
144 * @access public
145 * @param string
146 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200147 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000148if ( ! function_exists('reduce_double_slashes'))
149{
150 function reduce_double_slashes($str)
151 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200152 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154}
Derek Jones1e141922010-03-05 10:24:50 -0600155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156// ------------------------------------------------------------------------
157
158/**
159 * Reduce Multiples
160 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500161 * Reduces multiple instances of a particular character. Example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 *
163 * Fred, Bill,, Joe, Jimmy
164 *
165 * becomes:
166 *
167 * Fred, Bill, Joe, Jimmy
168 *
169 * @access public
170 * @param string
171 * @param string the character you wish to reduce
172 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
173 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200174 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000175if ( ! function_exists('reduce_multiples'))
176{
177 function reduce_multiples($str, $character = ',', $trim = FALSE)
178 {
179 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200180 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182}
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184// ------------------------------------------------------------------------
185
186/**
187 * Create a Random String
188 *
189 * Useful for generating passwords or hashes.
190 *
191 * @access public
Derek Jones37f4b9c2011-07-01 17:56:50 -0500192 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
Andrey Andreev09375d72012-01-19 14:57:46 +0200193 * @param int number of characters
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200195 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000196if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000197{
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200199 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200200 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200202 case 'basic': return mt_rand();
203 case 'alnum':
204 case 'numeric':
205 case 'nozero':
206 case 'alpha':
207 switch ($type)
208 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200209 case 'alpha':
210 $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200211 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200212 case 'alnum':
213 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200214 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200215 case 'numeric':
216 $pool = '0123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200217 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200218 case 'nozero':
219 $pool = '123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200220 break;
221 }
222 return substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len);
223 case 'unique':
224 case 'md5': return md5(uniqid(mt_rand()));
225 case 'encrypt':
226 case 'sha1':
227 $CI =& get_instance();
228 $CI->load->helper('security');
229 return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231 }
232}
Derek Jones1e141922010-03-05 10:24:50 -0600233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234// ------------------------------------------------------------------------
235
236/**
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600237 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
238 *
Andrey Andreev09375d72012-01-19 14:57:46 +0200239 * @param string $str required
240 * @param string $separator What should the duplicate number be appended with
241 * @param string $first Which number should be used for the first dupe increment
242 * @return string
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600243 */
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600244function increment_string($str, $separator = '_', $first = 1)
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600245{
246 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600247 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
248}
249
250// ------------------------------------------------------------------------
251
252/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * Alternator
254 *
Andrey Andreev09375d72012-01-19 14:57:46 +0200255 * Allows strings to be alternated. See docs...
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 *
257 * @access public
Andrey Andreev09375d72012-01-19 14:57:46 +0200258 * @param string (as many parameters as needed)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261if ( ! function_exists('alternator'))
262{
263 function alternator()
264 {
Barry Mienydd671972010-10-04 16:33:58 +0200265 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000266
267 if (func_num_args() == 0)
268 {
269 $i = 0;
270 return '';
271 }
272 $args = func_get_args();
273 return $args[($i++ % count($args))];
274 }
275}
276
277// ------------------------------------------------------------------------
278
279/**
280 * Repeater function
281 *
282 * @access public
283 * @param string
Andrey Andreev09375d72012-01-19 14:57:46 +0200284 * @param int number of repeats
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200286 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000287if ( ! function_exists('repeater'))
288{
289 function repeater($data, $num = 1)
290 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200291 return ($num > 0) ? str_repeat($data, $num) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293}
294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295/* End of file string_helper.php */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200296/* Location: ./system/helpers/string_helper.php */