blob: c895d57678a3bda8ae4d8f898e8ebb2179815525 [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 *
54 * @param string
55 * @return string
56 */
Derek Allard2067d1a2008-11-13 22:59:24 +000057 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
Derek Allard2067d1a2008-11-13 22:59:24 +000065if ( ! function_exists('strip_slashes'))
66{
Timothy Warrenb75faa12012-04-27 12:03:32 -040067 /**
68 * Strip Slashes
69 *
70 * Removes slashes contained in a string or in an array
71 *
72 * @param mixed string or array
73 * @return mixed string or array
74 */
Derek Allard2067d1a2008-11-13 22:59:24 +000075 function strip_slashes($str)
76 {
Andrey Andreev09375d72012-01-19 14:57:46 +020077 if ( ! is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020078 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020079 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000080 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Andrey Andreev09375d72012-01-19 14:57:46 +020082 foreach ($str as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Andrey Andreev09375d72012-01-19 14:57:46 +020084 $str[$key] = strip_slashes($val);
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
86
87 return $str;
88 }
89}
90
91// ------------------------------------------------------------------------
92
Derek Allard2067d1a2008-11-13 22:59:24 +000093if ( ! function_exists('strip_quotes'))
94{
Timothy Warrenb75faa12012-04-27 12:03:32 -040095 /**
96 * Strip Quotes
97 *
98 * Removes single and double quotes from a string
99 *
100 * @param string
101 * @return string
102 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 function strip_quotes($str)
104 {
105 return str_replace(array('"', "'"), '', $str);
106 }
107}
108
109// ------------------------------------------------------------------------
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111if ( ! function_exists('quotes_to_entities'))
112{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400113 /**
114 * Quotes to Entities
115 *
116 * Converts single and double quotes to entities
117 *
118 * @param string
119 * @return string
120 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200122 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
124 }
125}
126
127// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129if ( ! function_exists('reduce_double_slashes'))
130{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400131 /**
132 * Reduce Double Slashes
133 *
134 * Converts double slashes in a string to a single slash,
135 * except those found in http://
136 *
137 * http://www.some-site.com//index.php
138 *
139 * becomes:
140 *
141 * http://www.some-site.com/index.php
142 *
143 * @param string
144 * @return string
145 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 function reduce_double_slashes($str)
147 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300148 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150}
Derek Jones1e141922010-03-05 10:24:50 -0600151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152// ------------------------------------------------------------------------
153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154if ( ! function_exists('reduce_multiples'))
155{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400156 /**
157 * Reduce Multiples
158 *
159 * Reduces multiple instances of a particular character. Example:
160 *
161 * Fred, Bill,, Joe, Jimmy
162 *
163 * becomes:
164 *
165 * Fred, Bill, Joe, Jimmy
166 *
167 * @param string
168 * @param string the character you wish to reduce
169 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
170 * @return string
171 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 function reduce_multiples($str, $character = ',', $trim = FALSE)
173 {
174 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200175 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177}
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179// ------------------------------------------------------------------------
180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000182{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400183 /**
184 * Create a Random String
185 *
186 * Useful for generating passwords or hashes.
187 *
188 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
189 * @param int number of characters
190 * @return string
191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200194 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev1a361202012-03-26 22:08:49 +0300196 case 'basic':
197 return mt_rand();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200198 case 'alnum':
199 case 'numeric':
200 case 'nozero':
201 case 'alpha':
202 switch ($type)
203 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200204 case 'alpha':
205 $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200206 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200207 case 'alnum':
208 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200209 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200210 case 'numeric':
211 $pool = '0123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200212 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200213 case 'nozero':
214 $pool = '123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200215 break;
216 }
Andrey Andreev75124a52012-03-13 12:47:58 +0200217 return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200218 case 'unique':
Andrey Andreev1a361202012-03-26 22:08:49 +0300219 case 'md5':
220 return md5(uniqid(mt_rand()));
Andrey Andreev4921fed2012-01-07 01:28:07 +0200221 case 'encrypt':
222 case 'sha1':
Andrey Andreev1a361202012-03-26 22:08:49 +0300223 return sha1(uniqid(mt_rand(), TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225 }
226}
Derek Jones1e141922010-03-05 10:24:50 -0600227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228// ------------------------------------------------------------------------
229
Andrey Andreev84c3b272012-03-26 21:50:00 +0300230if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600231{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400232 /**
233 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
234 *
235 * @param string required
236 * @param string What should the duplicate number be appended with
237 * @param string Which number should be used for the first dupe increment
238 * @return string
239 */
Andrey Andreev84c3b272012-03-26 21:50:00 +0300240 function increment_string($str, $separator = '_', $first = 1)
241 {
242 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Andrey Andreev84c3b272012-03-26 21:50:00 +0300243 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
244 }
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600245}
246
247// ------------------------------------------------------------------------
248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249if ( ! function_exists('alternator'))
250{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400251 /**
252 * Alternator
253 *
254 * Allows strings to be alternated. See docs...
255 *
256 * @param string (as many parameters as needed)
257 * @return string
258 */
259 function alternator($args)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Barry Mienydd671972010-10-04 16:33:58 +0200261 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000262
Alex Bilbie773ccc32012-06-02 11:11:08 +0100263 if (func_num_args() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 $i = 0;
266 return '';
267 }
268 $args = func_get_args();
269 return $args[($i++ % count($args))];
270 }
271}
272
273// ------------------------------------------------------------------------
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275if ( ! function_exists('repeater'))
276{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400277 /**
278 * Repeater function
279 *
Andrey Andreev60826db2012-10-27 14:45:23 +0300280 * @todo Remove in version 3.1+.
281 * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat()
282 *
283 * @param string $data String to repeat
284 * @param int $num Number of repeats
Timothy Warrenb75faa12012-04-27 12:03:32 -0400285 * @return string
286 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 function repeater($data, $num = 1)
288 {
Andrey Andreev84c3b272012-03-26 21:50:00 +0300289 return ($num > 0) ? str_repeat($data, $num) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291}
292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293/* End of file string_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000294/* Location: ./system/helpers/string_helper.php */