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