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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 | e684bda | 2012-03-26 13:47:29 +0300 | [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 | e684bda | 2012-03-26 13:47:29 +0300 | [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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * CodeIgniter String Helpers |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Helpers |
| 34 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/helpers/string_helper.html |
| 37 | */ |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | if ( ! function_exists('trim_slashes')) |
| 42 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 43 | /** |
| 44 | * Trim Slashes |
| 45 | * |
| 46 | * Removes any leading/trailing slashes from a string: |
| 47 | * |
| 48 | * /this/that/theother/ |
| 49 | * |
| 50 | * becomes: |
| 51 | * |
| 52 | * this/that/theother |
| 53 | * |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 54 | * @todo Remove in version 3.1+. |
| 55 | * @deprecated 3.0.0 This is just an alias for PHP's native trim() |
| 56 | * |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 57 | * @param string |
| 58 | * @return string |
| 59 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | function trim_slashes($str) |
| 61 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | return trim($str, '/'); |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 63 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 65 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | // ------------------------------------------------------------------------ |
| 67 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | if ( ! function_exists('strip_slashes')) |
| 69 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 70 | /** |
| 71 | * Strip Slashes |
| 72 | * |
| 73 | * Removes slashes contained in a string or in an array |
| 74 | * |
| 75 | * @param mixed string or array |
| 76 | * @return mixed string or array |
| 77 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | function strip_slashes($str) |
| 79 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 80 | if ( ! is_array($str)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 82 | return stripslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 85 | foreach ($str as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 87 | $str[$key] = strip_slashes($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return $str; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // ------------------------------------------------------------------------ |
| 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | if ( ! function_exists('strip_quotes')) |
| 97 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Strip Quotes |
| 100 | * |
| 101 | * Removes single and double quotes from a string |
| 102 | * |
| 103 | * @param string |
| 104 | * @return string |
| 105 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | function strip_quotes($str) |
| 107 | { |
| 108 | return str_replace(array('"', "'"), '', $str); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | if ( ! function_exists('quotes_to_entities')) |
| 115 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 116 | /** |
| 117 | * Quotes to Entities |
| 118 | * |
| 119 | * Converts single and double quotes to entities |
| 120 | * |
| 121 | * @param string |
| 122 | * @return string |
| 123 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | if ( ! function_exists('reduce_double_slashes')) |
| 133 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [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 | * @param string |
| 147 | * @return string |
| 148 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | function reduce_double_slashes($str) |
| 150 | { |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 151 | return preg_replace('#(^|[^:])//+#', '\\1/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 154 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | // ------------------------------------------------------------------------ |
| 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | if ( ! function_exists('reduce_multiples')) |
| 158 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 159 | /** |
| 160 | * Reduce Multiples |
| 161 | * |
| 162 | * Reduces multiple instances of a particular character. Example: |
| 163 | * |
| 164 | * Fred, Bill,, Joe, Jimmy |
| 165 | * |
| 166 | * becomes: |
| 167 | * |
| 168 | * Fred, Bill, Joe, Jimmy |
| 169 | * |
| 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 |
| 174 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | function reduce_multiples($str, $character = ',', $trim = FALSE) |
| 176 | { |
| 177 | $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 178 | return ($trim === TRUE) ? trim($str, $character) : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | // ------------------------------------------------------------------------ |
| 183 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | if ( ! function_exists('random_string')) |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 185 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 186 | /** |
| 187 | * Create a Random String |
| 188 | * |
| 189 | * Useful for generating passwords or hashes. |
| 190 | * |
| 191 | * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 |
| 192 | * @param int number of characters |
| 193 | * @return string |
| 194 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | function random_string($type = 'alnum', $len = 8) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 197 | switch ($type) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | { |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 199 | case 'basic': |
| 200 | return mt_rand(); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 201 | case 'alnum': |
| 202 | case 'numeric': |
| 203 | case 'nozero': |
| 204 | case 'alpha': |
| 205 | switch ($type) |
| 206 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 207 | case 'alpha': |
| 208 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 209 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 210 | case 'alnum': |
| 211 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 212 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 213 | case 'numeric': |
| 214 | $pool = '0123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 215 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 216 | case 'nozero': |
| 217 | $pool = '123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 218 | break; |
| 219 | } |
Andrey Andreev | 75124a5 | 2012-03-13 12:47:58 +0200 | [diff] [blame] | 220 | 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^] | 221 | case 'unique': // todo: remove in 3.1+ |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 222 | case 'md5': |
| 223 | return md5(uniqid(mt_rand())); |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame^] | 224 | case 'encrypt': // todo: remove in 3.1+ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 225 | case 'sha1': |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 226 | return sha1(uniqid(mt_rand(), TRUE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | // ------------------------------------------------------------------------ |
| 232 | |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 233 | if ( ! function_exists('increment_string')) |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 234 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 235 | /** |
| 236 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
| 237 | * |
| 238 | * @param string required |
| 239 | * @param string What should the duplicate number be appended with |
| 240 | * @param string Which number should be used for the first dupe increment |
| 241 | * @return string |
| 242 | */ |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 243 | function increment_string($str, $separator = '_', $first = 1) |
| 244 | { |
| 245 | preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match); |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 246 | return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first; |
| 247 | } |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // ------------------------------------------------------------------------ |
| 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | if ( ! function_exists('alternator')) |
| 253 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 254 | /** |
| 255 | * Alternator |
| 256 | * |
| 257 | * Allows strings to be alternated. See docs... |
| 258 | * |
| 259 | * @param string (as many parameters as needed) |
| 260 | * @return string |
| 261 | */ |
| 262 | function alternator($args) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 264 | static $i; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 266 | if (func_num_args() === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | { |
| 268 | $i = 0; |
| 269 | return ''; |
| 270 | } |
| 271 | $args = func_get_args(); |
| 272 | return $args[($i++ % count($args))]; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // ------------------------------------------------------------------------ |
| 277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | if ( ! function_exists('repeater')) |
| 279 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 280 | /** |
| 281 | * Repeater function |
| 282 | * |
Andrey Andreev | 60826db | 2012-10-27 14:45:23 +0300 | [diff] [blame] | 283 | * @todo Remove in version 3.1+. |
| 284 | * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat() |
| 285 | * |
| 286 | * @param string $data String to repeat |
| 287 | * @param int $num Number of repeats |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 288 | * @return string |
| 289 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | function repeater($data, $num = 1) |
| 291 | { |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 292 | return ($num > 0) ? str_repeat($data, $num) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 293 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | /* End of file string_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 297 | /* Location: ./system/helpers/string_helper.php */ |