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 |
| 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 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 | { |
| 78 | if (is_array($str)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | foreach ($str as $key => $val) |
| 81 | { |
| 82 | $str[$key] = strip_slashes($val); |
| 83 | } |
| 84 | } |
| 85 | else |
| 86 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 87 | return stripslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | if ( ! 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | if ( ! function_exists('quotes_to_entities')) |
| 125 | { |
| 126 | function quotes_to_entities($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // ------------------------------------------------------------------------ |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | /** |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | if ( ! function_exists('reduce_double_slashes')) |
| 151 | { |
| 152 | function reduce_double_slashes($str) |
| 153 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame^] | 154 | return preg_replace('#(^|[^:])//+#', '\\1/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 157 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | // ------------------------------------------------------------------------ |
| 159 | |
| 160 | /** |
| 161 | * Reduce Multiples |
| 162 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 163 | * Reduces multiple instances of a particular character. Example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | * |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | if ( ! 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 Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 182 | return ($trim === TRUE) ? trim($str, $character) : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | // ------------------------------------------------------------------------ |
| 187 | |
| 188 | /** |
| 189 | * Create a Random String |
| 190 | * |
| 191 | * Useful for generating passwords or hashes. |
| 192 | * |
| 193 | * @access public |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 194 | * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | * @param integer number of characters |
| 196 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | if ( ! function_exists('random_string')) |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 199 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | function random_string($type = 'alnum', $len = 8) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 202 | switch ($type) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 204 | case 'basic': return mt_rand(); |
| 205 | case 'alnum': |
| 206 | case 'numeric': |
| 207 | case 'nozero': |
| 208 | case 'alpha': |
| 209 | switch ($type) |
| 210 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame^] | 211 | case 'alpha': |
| 212 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 213 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame^] | 214 | case 'alnum': |
| 215 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 216 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame^] | 217 | case 'numeric': |
| 218 | $pool = '0123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 219 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame^] | 220 | case 'nozero': |
| 221 | $pool = '123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 222 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | // ------------------------------------------------------------------------ |
| 237 | |
| 238 | /** |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 239 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
| 240 | * |
| 241 | * @param string $str required |
Phil Sturgeon | dbac73b | 2011-08-14 12:23:55 -0600 | [diff] [blame] | 242 | * @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 Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 244 | * @return string |
| 245 | */ |
Phil Sturgeon | dbac73b | 2011-08-14 12:23:55 -0600 | [diff] [blame] | 246 | function increment_string($str, $separator = '_', $first = 1) |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 247 | { |
| 248 | preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match); |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 249 | return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first; |
| 250 | } |
| 251 | |
| 252 | // ------------------------------------------------------------------------ |
| 253 | |
| 254 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | * Alternator |
| 256 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 257 | * Allows strings to be alternated. See docs... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | * |
| 259 | * @access public |
| 260 | * @param string (as many parameters as needed) |
| 261 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | if ( ! function_exists('alternator')) |
| 264 | { |
| 265 | function alternator() |
| 266 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | static $i; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | if ( ! function_exists('repeater')) |
| 290 | { |
| 291 | function repeater($data, $num = 1) |
| 292 | { |
| 293 | return (($num > 0) ? str_repeat($data, $num) : ''); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | /* End of file string_helper.php */ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 298 | /* Location: ./system/helpers/string_helper.php */ |