blob: 654f721b0fcdfba597d656521cf9101c06b631ed [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
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 */
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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020089 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
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
Andrey Andreev4921fed2012-01-07 01:28:07 +0200185 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187}
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189// ------------------------------------------------------------------------
190
191/**
192 * Create a Random String
193 *
194 * Useful for generating passwords or hashes.
195 *
196 * @access public
Derek Jones37f4b9c2011-07-01 17:56:50 -0500197 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * @param integer number of characters
199 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200200 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000201if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000202{
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200205 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200207 case 'basic': return mt_rand();
208 case 'alnum':
209 case 'numeric':
210 case 'nozero':
211 case 'alpha':
212 switch ($type)
213 {
214 case 'alpha': $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
215 break;
216 case 'alnum': $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
217 break;
218 case 'numeric': $pool = '0123456789';
219 break;
220 case 'nozero': $pool = '123456789';
221 break;
222 }
223 return substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len);
224 case 'unique':
225 case 'md5': return md5(uniqid(mt_rand()));
226 case 'encrypt':
227 case 'sha1':
228 $CI =& get_instance();
229 $CI->load->helper('security');
230 return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232 }
233}
Derek Jones1e141922010-03-05 10:24:50 -0600234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235// ------------------------------------------------------------------------
236
237/**
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600238 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
239 *
240 * @param string $str required
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600241 * @param string $separator What should the duplicate number be appended with
242 * @param string $first Which number should be used for the first dupe increment
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600243 * @return string
244 */
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600245function increment_string($str, $separator = '_', $first = 1)
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600246{
247 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600248 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
249}
250
251// ------------------------------------------------------------------------
252
253/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * Alternator
255 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500256 * Allows strings to be alternated. See docs...
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 *
258 * @access public
259 * @param string (as many parameters as needed)
260 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200261 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000262if ( ! function_exists('alternator'))
263{
264 function alternator()
265 {
Barry Mienydd671972010-10-04 16:33:58 +0200266 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267
268 if (func_num_args() == 0)
269 {
270 $i = 0;
271 return '';
272 }
273 $args = func_get_args();
274 return $args[($i++ % count($args))];
275 }
276}
277
278// ------------------------------------------------------------------------
279
280/**
281 * Repeater function
282 *
283 * @access public
284 * @param string
285 * @param integer number of repeats
286 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200287 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000288if ( ! function_exists('repeater'))
289{
290 function repeater($data, $num = 1)
291 {
292 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 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200297/* Location: ./system/helpers/string_helper.php */