blob: 6691681e0465641c1ff8d49e079433213a551787 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter String Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/string_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Trim Slashes
44 *
Derek Jones1e141922010-03-05 10:24:50 -060045 * Removes any leading/trailing slashes from a string:
Derek Allard2067d1a2008-11-13 22:59:24 +000046 *
47 * /this/that/theother/
48 *
49 * becomes:
50 *
51 * this/that/theother
52 *
53 * @access public
54 * @param string
55 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020056 */
Derek Allard2067d1a2008-11-13 22:59:24 +000057if ( ! function_exists('trim_slashes'))
58{
59 function trim_slashes($str)
60 {
Barry Mienydd671972010-10-04 16:33:58 +020061 return trim($str, '/');
Derek Jones1e141922010-03-05 10:24:50 -060062 }
Derek Allard2067d1a2008-11-13 22:59:24 +000063}
Derek Jones1e141922010-03-05 10:24:50 -060064
Derek Allard2067d1a2008-11-13 22:59:24 +000065// ------------------------------------------------------------------------
66
67/**
68 * Strip Slashes
69 *
70 * Removes slashes contained in a string or in an array
71 *
72 * @access public
73 * @param mixed string or array
74 * @return mixed string or array
Barry Mienydd671972010-10-04 16:33:58 +020075 */
Derek Allard2067d1a2008-11-13 22:59:24 +000076if ( ! function_exists('strip_slashes'))
77{
78 function strip_slashes($str)
79 {
80 if (is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020081 {
Derek Allard2067d1a2008-11-13 22:59:24 +000082 foreach ($str as $key => $val)
83 {
84 $str[$key] = strip_slashes($val);
85 }
86 }
87 else
88 {
89 $str = stripslashes($str);
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 return $str;
93 }
94}
95
96// ------------------------------------------------------------------------
97
98/**
99 * Strip Quotes
100 *
101 * Removes single and double quotes from a string
102 *
103 * @access public
104 * @param string
105 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200106 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000107if ( ! function_exists('strip_quotes'))
108{
109 function strip_quotes($str)
110 {
111 return str_replace(array('"', "'"), '', $str);
112 }
113}
114
115// ------------------------------------------------------------------------
116
117/**
118 * Quotes to Entities
119 *
120 * Converts single and double quotes to entities
121 *
122 * @access public
123 * @param string
124 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126if ( ! function_exists('quotes_to_entities'))
127{
128 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200129 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
131 }
132}
133
134// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136/**
137 * Reduce Double Slashes
138 *
139 * Converts double slashes in a string to a single slash,
140 * except those found in http://
141 *
142 * http://www.some-site.com//index.php
143 *
144 * becomes:
145 *
146 * http://www.some-site.com/index.php
147 *
148 * @access public
149 * @param string
150 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200151 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000152if ( ! function_exists('reduce_double_slashes'))
153{
154 function reduce_double_slashes($str)
155 {
Pascal Kriete42e74bc2009-12-23 16:44:07 +0000156 return preg_replace("#(^|[^:])//+#", "\\1/", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158}
Derek Jones1e141922010-03-05 10:24:50 -0600159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160// ------------------------------------------------------------------------
161
162/**
163 * Reduce Multiples
164 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500165 * Reduces multiple instances of a particular character. Example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 *
167 * Fred, Bill,, Joe, Jimmy
168 *
169 * becomes:
170 *
171 * Fred, Bill, Joe, Jimmy
172 *
173 * @access public
174 * @param string
175 * @param string the character you wish to reduce
176 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
177 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000179if ( ! function_exists('reduce_multiples'))
180{
181 function reduce_multiples($str, $character = ',', $trim = FALSE)
182 {
183 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
184
185 if ($trim === TRUE)
186 {
187 $str = trim($str, $character);
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 return $str;
191 }
192}
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194// ------------------------------------------------------------------------
195
196/**
197 * Create a Random String
198 *
199 * Useful for generating passwords or hashes.
200 *
201 * @access public
Derek Jones37f4b9c2011-07-01 17:56:50 -0500202 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param integer number of characters
204 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200205 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000206if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000207{
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 switch($type)
211 {
Derek Allard472dd212010-01-23 20:03:27 +0000212 case 'basic' : return mt_rand();
Barry Mienydd671972010-10-04 16:33:58 +0200213 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 case 'alnum' :
215 case 'numeric' :
216 case 'nozero' :
Derek Allard472dd212010-01-23 20:03:27 +0000217 case 'alpha' :
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 switch ($type)
220 {
Derek Allard472dd212010-01-23 20:03:27 +0000221 case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
222 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
224 break;
225 case 'numeric' : $pool = '0123456789';
226 break;
227 case 'nozero' : $pool = '123456789';
228 break;
229 }
druu9e2df7c2011-09-06 22:19:59 +0300230
231 $str = substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len);
232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 return $str;
Barry Mienydd671972010-10-04 16:33:58 +0200234 break;
235 case 'unique' :
236 case 'md5' :
237
Derek Allard472dd212010-01-23 20:03:27 +0000238 return md5(uniqid(mt_rand()));
Barry Mienydd671972010-10-04 16:33:58 +0200239 break;
240 case 'encrypt' :
241 case 'sha1' :
242
Derek Allard472dd212010-01-23 20:03:27 +0000243 $CI =& get_instance();
244 $CI->load->helper('security');
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard472dd212010-01-23 20:03:27 +0000246 return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
Barry Mienydd671972010-10-04 16:33:58 +0200247 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249 }
250}
Derek Jones1e141922010-03-05 10:24:50 -0600251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252// ------------------------------------------------------------------------
253
254/**
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600255 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
256 *
257 * @param string $str required
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600258 * @param string $separator What should the duplicate number be appended with
259 * @param string $first Which number should be used for the first dupe increment
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600260 * @return string
261 */
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600262function increment_string($str, $separator = '_', $first = 1)
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600263{
264 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
265
266 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
267}
268
269// ------------------------------------------------------------------------
270
271/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 * Alternator
273 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500274 * Allows strings to be alternated. See docs...
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 *
276 * @access public
277 * @param string (as many parameters as needed)
278 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200279 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000280if ( ! function_exists('alternator'))
281{
282 function alternator()
283 {
Barry Mienydd671972010-10-04 16:33:58 +0200284 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000285
286 if (func_num_args() == 0)
287 {
288 $i = 0;
289 return '';
290 }
291 $args = func_get_args();
292 return $args[($i++ % count($args))];
293 }
294}
295
296// ------------------------------------------------------------------------
297
298/**
299 * Repeater function
300 *
301 * @access public
302 * @param string
303 * @param integer number of repeats
304 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200305 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000306if ( ! function_exists('repeater'))
307{
308 function repeater($data, $num = 1)
309 {
310 return (($num > 0) ? str_repeat($data, $num) : '');
Barry Mienydd671972010-10-04 16:33:58 +0200311 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000312}
313
314
315/* End of file string_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000316/* Location: ./system/helpers/string_helper.php */