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 | * |
| 54 | * @param string |
| 55 | * @return string |
| 56 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | if ( ! function_exists('strip_slashes')) |
| 66 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 67 | /** |
| 68 | * Strip Slashes |
| 69 | * |
| 70 | * Removes slashes contained in a string or in an array |
| 71 | * |
| 72 | * @param mixed string or array |
| 73 | * @return mixed string or array |
| 74 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | function strip_slashes($str) |
| 76 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 77 | if ( ! is_array($str)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 79 | return stripslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 82 | foreach ($str as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 84 | $str[$key] = strip_slashes($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return $str; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // ------------------------------------------------------------------------ |
| 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | if ( ! function_exists('strip_quotes')) |
| 94 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 95 | /** |
| 96 | * Strip Quotes |
| 97 | * |
| 98 | * Removes single and double quotes from a string |
| 99 | * |
| 100 | * @param string |
| 101 | * @return string |
| 102 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | function strip_quotes($str) |
| 104 | { |
| 105 | return str_replace(array('"', "'"), '', $str); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // ------------------------------------------------------------------------ |
| 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | if ( ! function_exists('quotes_to_entities')) |
| 112 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 113 | /** |
| 114 | * Quotes to Entities |
| 115 | * |
| 116 | * Converts single and double quotes to entities |
| 117 | * |
| 118 | * @param string |
| 119 | * @return string |
| 120 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | function quotes_to_entities($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | if ( ! function_exists('reduce_double_slashes')) |
| 130 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 131 | /** |
| 132 | * Reduce Double Slashes |
| 133 | * |
| 134 | * Converts double slashes in a string to a single slash, |
| 135 | * except those found in http:// |
| 136 | * |
| 137 | * http://www.some-site.com//index.php |
| 138 | * |
| 139 | * becomes: |
| 140 | * |
| 141 | * http://www.some-site.com/index.php |
| 142 | * |
| 143 | * @param string |
| 144 | * @return string |
| 145 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | function reduce_double_slashes($str) |
| 147 | { |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 148 | return preg_replace('#(^|[^:])//+#', '\\1/', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | // ------------------------------------------------------------------------ |
| 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | if ( ! function_exists('reduce_multiples')) |
| 155 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 156 | /** |
| 157 | * Reduce Multiples |
| 158 | * |
| 159 | * Reduces multiple instances of a particular character. Example: |
| 160 | * |
| 161 | * Fred, Bill,, Joe, Jimmy |
| 162 | * |
| 163 | * becomes: |
| 164 | * |
| 165 | * Fred, Bill, Joe, Jimmy |
| 166 | * |
| 167 | * @param string |
| 168 | * @param string the character you wish to reduce |
| 169 | * @param bool TRUE/FALSE - whether to trim the character from the beginning/end |
| 170 | * @return string |
| 171 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | function reduce_multiples($str, $character = ',', $trim = FALSE) |
| 173 | { |
| 174 | $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 175 | return ($trim === TRUE) ? trim($str, $character) : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | // ------------------------------------------------------------------------ |
| 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | if ( ! function_exists('random_string')) |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 182 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 183 | /** |
| 184 | * Create a Random String |
| 185 | * |
| 186 | * Useful for generating passwords or hashes. |
| 187 | * |
| 188 | * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 |
| 189 | * @param int number of characters |
| 190 | * @return string |
| 191 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | function random_string($type = 'alnum', $len = 8) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 194 | switch ($type) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | { |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 196 | case 'basic': |
| 197 | return mt_rand(); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 198 | case 'alnum': |
| 199 | case 'numeric': |
| 200 | case 'nozero': |
| 201 | case 'alpha': |
| 202 | switch ($type) |
| 203 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 204 | case 'alpha': |
| 205 | $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 206 | break; |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 207 | case 'alnum': |
| 208 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
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 'numeric': |
| 211 | $pool = '0123456789'; |
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 'nozero': |
| 214 | $pool = '123456789'; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 215 | break; |
| 216 | } |
Andrey Andreev | 75124a5 | 2012-03-13 12:47:58 +0200 | [diff] [blame] | 217 | return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 218 | case 'unique': |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 219 | case 'md5': |
| 220 | return md5(uniqid(mt_rand())); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 221 | case 'encrypt': |
| 222 | case 'sha1': |
Andrey Andreev | 1a36120 | 2012-03-26 22:08:49 +0300 | [diff] [blame] | 223 | return sha1(uniqid(mt_rand(), TRUE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | // ------------------------------------------------------------------------ |
| 229 | |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 230 | if ( ! function_exists('increment_string')) |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 231 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 232 | /** |
| 233 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
| 234 | * |
| 235 | * @param string required |
| 236 | * @param string What should the duplicate number be appended with |
| 237 | * @param string Which number should be used for the first dupe increment |
| 238 | * @return string |
| 239 | */ |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 240 | function increment_string($str, $separator = '_', $first = 1) |
| 241 | { |
| 242 | preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match); |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 243 | return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first; |
| 244 | } |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // ------------------------------------------------------------------------ |
| 248 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | if ( ! function_exists('alternator')) |
| 250 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 251 | /** |
| 252 | * Alternator |
| 253 | * |
| 254 | * Allows strings to be alternated. See docs... |
| 255 | * |
| 256 | * @param string (as many parameters as needed) |
| 257 | * @return string |
| 258 | */ |
| 259 | function alternator($args) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | static $i; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 263 | if (func_num_args() === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
| 265 | $i = 0; |
| 266 | return ''; |
| 267 | } |
| 268 | $args = func_get_args(); |
| 269 | return $args[($i++ % count($args))]; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // ------------------------------------------------------------------------ |
| 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | if ( ! function_exists('repeater')) |
| 276 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 277 | /** |
| 278 | * Repeater function |
| 279 | * |
Andrey Andreev | 60826db | 2012-10-27 14:45:23 +0300 | [diff] [blame] | 280 | * @todo Remove in version 3.1+. |
| 281 | * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat() |
| 282 | * |
| 283 | * @param string $data String to repeat |
| 284 | * @param int $num Number of repeats |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 285 | * @return string |
| 286 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | function repeater($data, $num = 1) |
| 288 | { |
Andrey Andreev | 84c3b27 | 2012-03-26 21:50:00 +0300 | [diff] [blame] | 289 | return ($num > 0) ? str_repeat($data, $num) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | /* End of file string_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 294 | /* Location: ./system/helpers/string_helper.php */ |