Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter String Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/string_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
| 41 | * Trim Slashes |
| 42 | * |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 43 | * Removes any leading/trailing slashes from a string: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | * |
| 45 | * /this/that/theother/ |
| 46 | * |
| 47 | * becomes: |
| 48 | * |
| 49 | * this/that/theother |
| 50 | * |
| 51 | * @access public |
| 52 | * @param string |
| 53 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | if ( ! function_exists('trim_slashes')) |
| 56 | { |
| 57 | function trim_slashes($str) |
| 58 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | return trim($str, '/'); |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 60 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 62 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | // ------------------------------------------------------------------------ |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | if ( ! function_exists('strip_slashes')) |
| 75 | { |
| 76 | function strip_slashes($str) |
| 77 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 78 | if ( ! is_array($str)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 80 | return stripslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 82 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 83 | foreach ($str as $key => $val) |
| 84 | { |
| 85 | $str[$key] = strip_slashes($val); |
| 86 | } |
| 87 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | return $str; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // ------------------------------------------------------------------------ |
| 93 | |
| 94 | /** |
| 95 | * Strip Quotes |
| 96 | * |
| 97 | * Removes single and double quotes from a string |
| 98 | * |
| 99 | * @access public |
| 100 | * @param string |
| 101 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | if ( ! function_exists('strip_quotes')) |
| 104 | { |
| 105 | function strip_quotes($str) |
| 106 | { |
| 107 | return str_replace(array('"', "'"), '', $str); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // ------------------------------------------------------------------------ |
| 112 | |
| 113 | /** |
| 114 | * Quotes to Entities |
| 115 | * |
| 116 | * Converts single and double quotes to entities |
| 117 | * |
| 118 | * @access public |
| 119 | * @param string |
| 120 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | if ( ! function_exists('quotes_to_entities')) |
| 123 | { |
| 124 | function quotes_to_entities($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // ------------------------------------------------------------------------ |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Reduce Double Slashes |
| 134 | * |
| 135 | * Converts double slashes in a string to a single slash, |
| 136 | * except those found in http:// |
| 137 | * |
| 138 | * http://www.some-site.com//index.php |
| 139 | * |
| 140 | * becomes: |
| 141 | * |
| 142 | * http://www.some-site.com/index.php |
| 143 | * |
| 144 | * @access public |
| 145 | * @param string |
| 146 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 147 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | if ( ! function_exists('reduce_double_slashes')) |
| 149 | { |
| 150 | function reduce_double_slashes($str) |
| 151 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 152 | return preg_replace('#(^|[^:])//+#', '\\1/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | // ------------------------------------------------------------------------ |
| 157 | |
| 158 | /** |
| 159 | * Reduce Multiples |
| 160 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 161 | * Reduces multiple instances of a particular character. Example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | * |
| 163 | * Fred, Bill,, Joe, Jimmy |
| 164 | * |
| 165 | * becomes: |
| 166 | * |
| 167 | * Fred, Bill, Joe, Jimmy |
| 168 | * |
| 169 | * @access public |
| 170 | * @param string |
| 171 | * @param string the character you wish to reduce |
| 172 | * @param bool TRUE/FALSE - whether to trim the character from the beginning/end |
| 173 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | if ( ! function_exists('reduce_multiples')) |
| 176 | { |
| 177 | function reduce_multiples($str, $character = ',', $trim = FALSE) |
| 178 | { |
| 179 | $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 180 | return ($trim === TRUE) ? trim($str, $character) : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | // ------------------------------------------------------------------------ |
| 185 | |
| 186 | /** |
| 187 | * Create a Random String |
| 188 | * |
| 189 | * Useful for generating passwords or hashes. |
| 190 | * |
| 191 | * @access public |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 192 | * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 193 | * @param int number of characters |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | if ( ! function_exists('random_string')) |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 197 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | function random_string($type = 'alnum', $len = 8) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 200 | switch ($type) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 202 | case 'basic': return mt_rand(); |
| 203 | case 'alnum': |
| 204 | case 'numeric': |
| 205 | case 'nozero': |
| 206 | case 'alpha': |
| 207 | switch ($type) |
| 208 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 209 | case 'alpha': |
| 210 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 211 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 212 | case 'alnum': |
| 213 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 214 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 215 | case 'numeric': |
| 216 | $pool = '0123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 217 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 218 | case 'nozero': |
| 219 | $pool = '123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | return substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len); |
| 223 | case 'unique': |
| 224 | case 'md5': return md5(uniqid(mt_rand())); |
| 225 | case 'encrypt': |
| 226 | case 'sha1': |
| 227 | $CI =& get_instance(); |
| 228 | $CI->load->helper('security'); |
| 229 | return do_hash(uniqid(mt_rand(), TRUE), 'sha1'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 233 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | // ------------------------------------------------------------------------ |
| 235 | |
| 236 | /** |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 237 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
| 238 | * |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 239 | * @param string $str required |
| 240 | * @param string $separator What should the duplicate number be appended with |
| 241 | * @param string $first Which number should be used for the first dupe increment |
| 242 | * @return string |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 243 | */ |
Phil Sturgeon | dbac73b | 2011-08-14 12:23:55 -0600 | [diff] [blame] | 244 | function increment_string($str, $separator = '_', $first = 1) |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 245 | { |
| 246 | preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match); |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 247 | return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first; |
| 248 | } |
| 249 | |
| 250 | // ------------------------------------------------------------------------ |
| 251 | |
| 252 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | * Alternator |
| 254 | * |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 255 | * Allows strings to be alternated. See docs... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | * |
| 257 | * @access public |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 258 | * @param string (as many parameters as needed) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | if ( ! function_exists('alternator')) |
| 262 | { |
| 263 | function alternator() |
| 264 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | static $i; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | |
| 267 | if (func_num_args() == 0) |
| 268 | { |
| 269 | $i = 0; |
| 270 | return ''; |
| 271 | } |
| 272 | $args = func_get_args(); |
| 273 | return $args[($i++ % count($args))]; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // ------------------------------------------------------------------------ |
| 278 | |
| 279 | /** |
| 280 | * Repeater function |
| 281 | * |
| 282 | * @access public |
| 283 | * @param string |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 284 | * @param int number of repeats |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | if ( ! function_exists('repeater')) |
| 288 | { |
| 289 | function repeater($data, $num = 1) |
| 290 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame^] | 291 | return ($num > 0) ? str_repeat($data, $num) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 292 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | /* End of file string_helper.php */ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 296 | /* Location: ./system/helpers/string_helper.php */ |