blob: 5177a7e5937219836eac0bb81481285bfbdf2ebe [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter String Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/string_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('trim_slashes'))
42{
Timothy Warrenb75faa12012-04-27 12:03:32 -040043 /**
44 * Trim Slashes
45 *
46 * Removes any leading/trailing slashes from a string:
47 *
48 * /this/that/theother/
49 *
50 * becomes:
51 *
52 * this/that/theother
53 *
Andrey Andreevf7c39d62012-11-08 22:01:33 +020054 * @todo Remove in version 3.1+.
55 * @deprecated 3.0.0 This is just an alias for PHP's native trim()
56 *
Timothy Warrenb75faa12012-04-27 12:03:32 -040057 * @param string
58 * @return string
59 */
Derek Allard2067d1a2008-11-13 22:59:24 +000060 function trim_slashes($str)
61 {
Barry Mienydd671972010-10-04 16:33:58 +020062 return trim($str, '/');
Derek Jones1e141922010-03-05 10:24:50 -060063 }
Derek Allard2067d1a2008-11-13 22:59:24 +000064}
Derek Jones1e141922010-03-05 10:24:50 -060065
Derek Allard2067d1a2008-11-13 22:59:24 +000066// ------------------------------------------------------------------------
67
Derek Allard2067d1a2008-11-13 22:59:24 +000068if ( ! function_exists('strip_slashes'))
69{
Timothy Warrenb75faa12012-04-27 12:03:32 -040070 /**
71 * Strip Slashes
72 *
73 * Removes slashes contained in a string or in an array
74 *
75 * @param mixed string or array
76 * @return mixed string or array
77 */
Derek Allard2067d1a2008-11-13 22:59:24 +000078 function strip_slashes($str)
79 {
Andrey Andreev09375d72012-01-19 14:57:46 +020080 if ( ! is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020081 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020082 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Andrey Andreev09375d72012-01-19 14:57:46 +020085 foreach ($str as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
Andrey Andreev09375d72012-01-19 14:57:46 +020087 $str[$key] = strip_slashes($val);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
89
90 return $str;
91 }
92}
93
94// ------------------------------------------------------------------------
95
Derek Allard2067d1a2008-11-13 22:59:24 +000096if ( ! function_exists('strip_quotes'))
97{
Timothy Warrenb75faa12012-04-27 12:03:32 -040098 /**
99 * Strip Quotes
100 *
101 * Removes single and double quotes from a string
102 *
103 * @param string
104 * @return string
105 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 function strip_quotes($str)
107 {
108 return str_replace(array('"', "'"), '', $str);
109 }
110}
111
112// ------------------------------------------------------------------------
113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114if ( ! function_exists('quotes_to_entities'))
115{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400116 /**
117 * Quotes to Entities
118 *
119 * Converts single and double quotes to entities
120 *
121 * @param string
122 * @return string
123 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 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 +0000132if ( ! function_exists('reduce_double_slashes'))
133{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400134 /**
135 * Reduce Double Slashes
136 *
137 * Converts double slashes in a string to a single slash,
138 * except those found in http://
139 *
140 * http://www.some-site.com//index.php
141 *
142 * becomes:
143 *
144 * http://www.some-site.com/index.php
145 *
146 * @param string
147 * @return string
148 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 function reduce_double_slashes($str)
150 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300151 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153}
Derek Jones1e141922010-03-05 10:24:50 -0600154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155// ------------------------------------------------------------------------
156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157if ( ! function_exists('reduce_multiples'))
158{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400159 /**
160 * Reduce Multiples
161 *
162 * Reduces multiple instances of a particular character. Example:
163 *
164 * Fred, Bill,, Joe, Jimmy
165 *
166 * becomes:
167 *
168 * Fred, Bill, Joe, Jimmy
169 *
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
174 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 function reduce_multiples($str, $character = ',', $trim = FALSE)
176 {
177 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200178 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180}
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182// ------------------------------------------------------------------------
183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000185{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400186 /**
187 * Create a Random String
188 *
189 * Useful for generating passwords or hashes.
190 *
191 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
192 * @param int number of characters
193 * @return string
194 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200197 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev1a361202012-03-26 22:08:49 +0300199 case 'basic':
200 return mt_rand();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200201 case 'alnum':
202 case 'numeric':
203 case 'nozero':
204 case 'alpha':
205 switch ($type)
206 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200207 case 'alpha':
208 $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200209 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200210 case 'alnum':
211 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200212 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200213 case 'numeric':
214 $pool = '0123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200215 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200216 case 'nozero':
217 $pool = '123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200218 break;
219 }
Andrey Andreev75124a52012-03-13 12:47:58 +0200220 return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200221 case 'unique':
Andrey Andreev1a361202012-03-26 22:08:49 +0300222 case 'md5':
223 return md5(uniqid(mt_rand()));
Andrey Andreev4921fed2012-01-07 01:28:07 +0200224 case 'encrypt':
225 case 'sha1':
Andrey Andreev1a361202012-03-26 22:08:49 +0300226 return sha1(uniqid(mt_rand(), TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228 }
229}
Derek Jones1e141922010-03-05 10:24:50 -0600230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231// ------------------------------------------------------------------------
232
Andrey Andreev84c3b272012-03-26 21:50:00 +0300233if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600234{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400235 /**
236 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
237 *
238 * @param string required
239 * @param string What should the duplicate number be appended with
240 * @param string Which number should be used for the first dupe increment
241 * @return string
242 */
Andrey Andreev84c3b272012-03-26 21:50:00 +0300243 function increment_string($str, $separator = '_', $first = 1)
244 {
245 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Andrey Andreev84c3b272012-03-26 21:50:00 +0300246 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
247 }
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600248}
249
250// ------------------------------------------------------------------------
251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252if ( ! function_exists('alternator'))
253{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400254 /**
255 * Alternator
256 *
257 * Allows strings to be alternated. See docs...
258 *
259 * @param string (as many parameters as needed)
260 * @return string
261 */
262 function alternator($args)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
Barry Mienydd671972010-10-04 16:33:58 +0200264 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000265
Alex Bilbie773ccc32012-06-02 11:11:08 +0100266 if (func_num_args() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 $i = 0;
269 return '';
270 }
271 $args = func_get_args();
272 return $args[($i++ % count($args))];
273 }
274}
275
276// ------------------------------------------------------------------------
277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278if ( ! function_exists('repeater'))
279{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400280 /**
281 * Repeater function
282 *
Andrey Andreev60826db2012-10-27 14:45:23 +0300283 * @todo Remove in version 3.1+.
284 * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat()
285 *
286 * @param string $data String to repeat
287 * @param int $num Number of repeats
Timothy Warrenb75faa12012-04-27 12:03:32 -0400288 * @return string
289 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 function repeater($data, $num = 1)
291 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300292 return ($num > 0) ? str_repeat($data, $num) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294}
295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296/* End of file string_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000297/* Location: ./system/helpers/string_helper.php */