blob: fcdb0aa845bbbf3cde14acd6d6d116b78ba66039 [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
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 *
51 * @access public
52 * @param string
53 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Derek Allard2067d1a2008-11-13 22:59:24 +000055if ( ! function_exists('trim_slashes'))
56{
57 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
65/**
66 * Strip Slashes
67 *
68 * Removes slashes contained in a string or in an array
69 *
70 * @access public
71 * @param mixed string or array
72 * @return mixed string or array
Barry Mienydd671972010-10-04 16:33:58 +020073 */
Derek Allard2067d1a2008-11-13 22:59:24 +000074if ( ! function_exists('strip_slashes'))
75{
76 function strip_slashes($str)
77 {
78 if (is_array($str))
Barry Mienydd671972010-10-04 16:33:58 +020079 {
Derek Allard2067d1a2008-11-13 22:59:24 +000080 foreach ($str as $key => $val)
81 {
82 $str[$key] = strip_slashes($val);
83 }
84 }
85 else
86 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020087 return stripslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 return $str;
91 }
92}
93
94// ------------------------------------------------------------------------
95
96/**
97 * Strip Quotes
98 *
99 * Removes single and double quotes from a string
100 *
101 * @access public
102 * @param string
103 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200104 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000105if ( ! function_exists('strip_quotes'))
106{
107 function strip_quotes($str)
108 {
109 return str_replace(array('"', "'"), '', $str);
110 }
111}
112
113// ------------------------------------------------------------------------
114
115/**
116 * Quotes to Entities
117 *
118 * Converts single and double quotes to entities
119 *
120 * @access public
121 * @param string
122 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200123 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000124if ( ! function_exists('quotes_to_entities'))
125{
126 function quotes_to_entities($str)
Barry Mienydd671972010-10-04 16:33:58 +0200127 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
129 }
130}
131
132// ------------------------------------------------------------------------
Derek Jones1e141922010-03-05 10:24:50 -0600133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134/**
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 * @access public
147 * @param string
148 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000150if ( ! function_exists('reduce_double_slashes'))
151{
152 function reduce_double_slashes($str)
153 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200154 return preg_replace('#(^|[^:])//+#', '\\1/', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156}
Derek Jones1e141922010-03-05 10:24:50 -0600157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158// ------------------------------------------------------------------------
159
160/**
161 * Reduce Multiples
162 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500163 * Reduces multiple instances of a particular character. Example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 *
165 * Fred, Bill,, Joe, Jimmy
166 *
167 * becomes:
168 *
169 * Fred, Bill, Joe, Jimmy
170 *
171 * @access public
172 * @param string
173 * @param string the character you wish to reduce
174 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
175 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000177if ( ! function_exists('reduce_multiples'))
178{
179 function reduce_multiples($str, $character = ',', $trim = FALSE)
180 {
181 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
Andrey Andreev4921fed2012-01-07 01:28:07 +0200182 return ($trim === TRUE) ? trim($str, $character) : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184}
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186// ------------------------------------------------------------------------
187
188/**
189 * Create a Random String
190 *
191 * Useful for generating passwords or hashes.
192 *
193 * @access public
Derek Jones37f4b9c2011-07-01 17:56:50 -0500194 * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @param integer number of characters
196 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200197 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000198if ( ! function_exists('random_string'))
Derek Allard472dd212010-01-23 20:03:27 +0000199{
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 function random_string($type = 'alnum', $len = 8)
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200202 switch ($type)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200204 case 'basic': return mt_rand();
205 case 'alnum':
206 case 'numeric':
207 case 'nozero':
208 case 'alpha':
209 switch ($type)
210 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200211 case 'alpha':
212 $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200213 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200214 case 'alnum':
215 $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200216 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200217 case 'numeric':
218 $pool = '0123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200219 break;
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200220 case 'nozero':
221 $pool = '123456789';
Andrey Andreev4921fed2012-01-07 01:28:07 +0200222 break;
223 }
224 return substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len);
225 case 'unique':
226 case 'md5': return md5(uniqid(mt_rand()));
227 case 'encrypt':
228 case 'sha1':
229 $CI =& get_instance();
230 $CI->load->helper('security');
231 return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233 }
234}
Derek Jones1e141922010-03-05 10:24:50 -0600235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236// ------------------------------------------------------------------------
237
238/**
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600239 * Add's _1 to a string or increment the ending number to allow _2, _3, etc
240 *
241 * @param string $str required
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600242 * @param string $separator What should the duplicate number be appended with
243 * @param string $first Which number should be used for the first dupe increment
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600244 * @return string
245 */
Phil Sturgeondbac73b2011-08-14 12:23:55 -0600246function increment_string($str, $separator = '_', $first = 1)
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600247{
248 preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
Phil Sturgeond8ef6302011-08-14 12:10:55 -0600249 return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
250}
251
252// ------------------------------------------------------------------------
253
254/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * Alternator
256 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500257 * Allows strings to be alternated. See docs...
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 *
259 * @access public
260 * @param string (as many parameters as needed)
261 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000263if ( ! function_exists('alternator'))
264{
265 function alternator()
266 {
Barry Mienydd671972010-10-04 16:33:58 +0200267 static $i;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
269 if (func_num_args() == 0)
270 {
271 $i = 0;
272 return '';
273 }
274 $args = func_get_args();
275 return $args[($i++ % count($args))];
276 }
277}
278
279// ------------------------------------------------------------------------
280
281/**
282 * Repeater function
283 *
284 * @access public
285 * @param string
286 * @param integer number of repeats
287 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200288 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000289if ( ! function_exists('repeater'))
290{
291 function repeater($data, $num = 1)
292 {
293 return (($num > 0) ? str_repeat($data, $num) : '');
Barry Mienydd671972010-10-04 16:33:58 +0200294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295}
296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297/* End of file string_helper.php */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200298/* Location: ./system/helpers/string_helper.php */