blob: a6a6b485df5be9e572a027a96c672020db9c3db3 [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 *
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 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 Andreevc5a1f932012-01-24 15:29:02 +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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @param string
52 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020053 */
Derek Allard2067d1a2008-11-13 22:59:24 +000054if ( ! function_exists('trim_slashes'))
55{
56 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
64/**
65 * Strip Slashes
66 *
67 * Removes slashes contained in a string or in an array
68 *
Derek Allard2067d1a2008-11-13 22:59:24 +000069 * @param mixed string or array
70 * @return mixed string or array
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('strip_slashes'))
73{
74 function strip_slashes($str)
75 {
Andrey Andreev09375d72012-01-19 14:57:46 +020076 if ( ! is_array($str))
Derek Allard2067d1a2008-11-13 22:59:24 +000077 {
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)
82 {
83 $str[$key] = strip_slashes($val);
84 }
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 return $str;
87 }
88}
89
90// ------------------------------------------------------------------------
91
92/**
93 * Strip Quotes
94 *
95 * Removes single and double quotes from a string
96 *
Derek Allard2067d1a2008-11-13 22:59:24 +000097 * @param string
98 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000100if ( ! function_exists('strip_quotes'))
101{
102 function strip_quotes($str)
103 {
104 return str_replace(array('"', "'"), '', $str);
105 }
106}
107
108// ------------------------------------------------------------------------
109
110/**
111 * Quotes to Entities
112 *
113 * Converts single and double quotes to entities
114 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @param string
116 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000118if ( ! function_exists('quotes_to_entities'))
119{
120 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 +0000128/**
129 * Reduce Double Slashes
130 *
131 * Converts double slashes in a string to a single slash,
132 * except those found in http://
133 *
134 * http://www.some-site.com//index.php
135 *
136 * becomes:
137 *
138 * http://www.some-site.com/index.php
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string
141 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200142 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000143if ( ! function_exists('reduce_double_slashes'))
144{
145 function reduce_double_slashes($str)
146 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200147 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
153/**
154 * Reduce Multiples
155 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500156 * Reduces multiple instances of a particular character. Example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 *
158 * Fred, Bill,, Joe, Jimmy
159 *
160 * becomes:
161 *
162 * Fred, Bill, Joe, Jimmy
163 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 * @param string
165 * @param string the character you wish to reduce
166 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
167 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200168 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000169if ( ! function_exists('reduce_multiples'))
170{
171 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
180/**
181 * Create a Random String
182 *
183 * Useful for generating passwords or hashes.
184 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500185 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
Andrey Andreev09375d72012-01-19 14:57:46 +0200186 * @param int number of characters
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000189if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000190{
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
229/**
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600230 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
231 *
Andrey Andreev84c3b272012-03-26 21:50:00 +0300232 * @param string required
233 * @param string What should the duplicate number be appended with
234 * @param string Which number should be used for the first dupe increment
Andrey Andreev09375d72012-01-19 14:57:46 +0200235 * @return string
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600236 */
Andrey Andreev75124a52012-03-13 12:47:58 +0200237if ( ! function_exists('increment_string'))
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600238{
Andrey Andreev75124a52012-03-13 12:47:58 +0200239 function increment_string($str, $separator = '_', $first = 1)
240 {
241 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
242 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
248/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * Alternator
250 *
Andrey Andreev09375d72012-01-19 14:57:46 +0200251 * Allows strings to be alternated. See docs...
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 *
Andrey Andreev09375d72012-01-19 14:57:46 +0200253 * @param string (as many parameters as needed)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200255 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000256if ( ! function_exists('alternator'))
257{
258 function alternator()
259 {
Barry Mienydd671972010-10-04 16:33:58 +0200260 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
262 if (func_num_args() == 0)
263 {
264 $i = 0;
265 return '';
266 }
267 $args = func_get_args();
268 return $args[($i++ % count($args))];
269 }
270}
271
272// ------------------------------------------------------------------------
273
274/**
275 * Repeater function
276 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @param string
Andrey Andreev09375d72012-01-19 14:57:46 +0200278 * @param int number of repeats
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200280 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000281if ( ! function_exists('repeater'))
282{
283 function repeater($data, $num = 1)
284 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200285 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 */
Andrey Andreeve79a3b12012-03-20 15:41:22 +0200290/* Location: ./system/helpers/string_helper.php */