Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter String Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/helpers/string_helper.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | if ( ! function_exists('trim_slashes')) |
| 53 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Trim Slashes |
| 56 | * |
| 57 | * Removes any leading/trailing slashes from a string: |
| 58 | * |
| 59 | * /this/that/theother/ |
| 60 | * |
| 61 | * becomes: |
| 62 | * |
| 63 | * this/that/theother |
| 64 | * |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 65 | * @todo Remove in version 3.1+. |
| 66 | * @deprecated 3.0.0 This is just an alias for PHP's native trim() |
| 67 | * |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 68 | * @param string |
| 69 | * @return string |
| 70 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | function trim_slashes($str) |
| 72 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | return trim($str, '/'); |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 74 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 76 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | // ------------------------------------------------------------------------ |
| 78 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | if ( ! function_exists('strip_slashes')) |
| 80 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 81 | /** |
| 82 | * Strip Slashes |
| 83 | * |
| 84 | * Removes slashes contained in a string or in an array |
| 85 | * |
| 86 | * @param mixed string or array |
| 87 | * @return mixed string or array |
| 88 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | function strip_slashes($str) |
| 90 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 91 | if ( ! is_array($str)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 93 | return stripslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 96 | foreach ($str as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 98 | $str[$key] = strip_slashes($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return $str; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // ------------------------------------------------------------------------ |
| 106 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | if ( ! function_exists('strip_quotes')) |
| 108 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 109 | /** |
| 110 | * Strip Quotes |
| 111 | * |
| 112 | * Removes single and double quotes from a string |
| 113 | * |
| 114 | * @param string |
| 115 | * @return string |
| 116 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | function strip_quotes($str) |
| 118 | { |
| 119 | return str_replace(array('"', "'"), '', $str); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // ------------------------------------------------------------------------ |
| 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | if ( ! function_exists('quotes_to_entities')) |
| 126 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 127 | /** |
| 128 | * Quotes to Entities |
| 129 | * |
| 130 | * Converts single and double quotes to entities |
| 131 | * |
| 132 | * @param string |
| 133 | * @return string |
| 134 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | function quotes_to_entities($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // ------------------------------------------------------------------------ |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | if ( ! function_exists('reduce_double_slashes')) |
| 144 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Reduce Double Slashes |
| 147 | * |
| 148 | * Converts double slashes in a string to a single slash, |
| 149 | * except those found in http:// |
| 150 | * |
| 151 | * http://www.some-site.com//index.php |
| 152 | * |
| 153 | * becomes: |
| 154 | * |
| 155 | * http://www.some-site.com/index.php |
| 156 | * |
| 157 | * @param string |
| 158 | * @return string |
| 159 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | function reduce_double_slashes($str) |
| 161 | { |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 162 | return preg_replace('#(^|[^:])//+#', '\\1/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 165 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | // ------------------------------------------------------------------------ |
| 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | if ( ! function_exists('reduce_multiples')) |
| 169 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 170 | /** |
| 171 | * Reduce Multiples |
| 172 | * |
| 173 | * Reduces multiple instances of a particular character. Example: |
| 174 | * |
| 175 | * Fred, Bill,, Joe, Jimmy |
| 176 | * |
| 177 | * becomes: |
| 178 | * |
| 179 | * Fred, Bill, Joe, Jimmy |
| 180 | * |
| 181 | * @param string |
| 182 | * @param string the character you wish to reduce |
| 183 | * @param bool TRUE/FALSE - whether to trim the character from the beginning/end |
| 184 | * @return string |
| 185 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | function reduce_multiples($str, $character = ',', $trim = FALSE) |
| 187 | { |
| 188 | $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 189 | return ($trim === TRUE) ? trim($str, $character) : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | // ------------------------------------------------------------------------ |
| 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | if ( ! function_exists('random_string')) |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 196 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 197 | /** |
Andrey Andreev | 618870f | 2017-03-01 14:09:26 +0200 | [diff] [blame] | 198 | * Create a "Random" String |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 199 | * |
Olanipekun Femi | 08dd57c | 2014-02-07 08:16:48 -0800 | [diff] [blame] | 200 | * @param string type of random string. basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1 |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 201 | * @param int number of characters |
| 202 | * @return string |
| 203 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | function random_string($type = 'alnum', $len = 8) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 206 | switch ($type) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | { |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 208 | case 'basic': |
| 209 | return mt_rand(); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 210 | case 'alnum': |
| 211 | case 'numeric': |
| 212 | case 'nozero': |
| 213 | case 'alpha': |
| 214 | switch ($type) |
| 215 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 216 | case 'alpha': |
| 217 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 218 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 219 | case 'alnum': |
| 220 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 221 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 222 | case 'numeric': |
| 223 | $pool = '0123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 224 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 225 | case 'nozero': |
| 226 | $pool = '123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 227 | break; |
| 228 | } |
Andrey Andreev | 75124a5 | 2012-03-13 12:47:58 +0200 | [diff] [blame] | 229 | return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len); |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 230 | case 'unique': // todo: remove in 3.1+ |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 231 | case 'md5': |
| 232 | return md5(uniqid(mt_rand())); |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 233 | case 'encrypt': // todo: remove in 3.1+ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 234 | case 'sha1': |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 235 | return sha1(uniqid(mt_rand(), TRUE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | // ------------------------------------------------------------------------ |
| 241 | |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 242 | if ( ! function_exists('increment_string')) |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 243 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 244 | /** |
| 245 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
| 246 | * |
| 247 | * @param string required |
| 248 | * @param string What should the duplicate number be appended with |
| 249 | * @param string Which number should be used for the first dupe increment |
| 250 | * @return string |
| 251 | */ |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 252 | function increment_string($str, $separator = '_', $first = 1) |
| 253 | { |
Andrey Andreev | 9bb9469 | 2015-08-07 13:29:11 +0300 | [diff] [blame] | 254 | preg_match('/(.+)'.preg_quote($separator, '/').'([0-9]+)$/', $str, $match); |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 255 | return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first; |
| 256 | } |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // ------------------------------------------------------------------------ |
| 260 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | if ( ! function_exists('alternator')) |
| 262 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 263 | /** |
| 264 | * Alternator |
| 265 | * |
| 266 | * Allows strings to be alternated. See docs... |
| 267 | * |
| 268 | * @param string (as many parameters as needed) |
| 269 | * @return string |
| 270 | */ |
Andrey Andreev | a18c609 | 2015-12-03 17:53:14 +0200 | [diff] [blame] | 271 | function alternator() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | static $i; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 275 | if (func_num_args() === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | { |
| 277 | $i = 0; |
| 278 | return ''; |
| 279 | } |
Andrey Andreev | a18c609 | 2015-12-03 17:53:14 +0200 | [diff] [blame] | 280 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | $args = func_get_args(); |
| 282 | return $args[($i++ % count($args))]; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // ------------------------------------------------------------------------ |
| 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | if ( ! function_exists('repeater')) |
| 289 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 290 | /** |
| 291 | * Repeater function |
| 292 | * |
Andrey Andreev | 60826db | 2012-10-27 14:45:23 +0300 | [diff] [blame] | 293 | * @todo Remove in version 3.1+. |
| 294 | * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat() |
| 295 | * |
| 296 | * @param string $data String to repeat |
| 297 | * @param int $num Number of repeats |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 298 | * @return string |
| 299 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | function repeater($data, $num = 1) |
| 301 | { |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 302 | return ($num > 0) ? str_repeat($data, $num) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | } |