Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter String Helpers |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Helpers |
| 35 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/helpers/string_helper.html |
| 38 | */ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
| 43 | * Trim Slashes |
| 44 | * |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 45 | * Removes any leading/trailing slashes from a string: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | * |
| 47 | * /this/that/theother/ |
| 48 | * |
| 49 | * becomes: |
| 50 | * |
| 51 | * this/that/theother |
| 52 | * |
| 53 | * @access public |
| 54 | * @param string |
| 55 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 56 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | if ( ! function_exists('trim_slashes')) |
| 58 | { |
| 59 | function trim_slashes($str) |
| 60 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | return trim($str, '/'); |
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 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | // ------------------------------------------------------------------------ |
| 66 | |
| 67 | /** |
| 68 | * Strip Slashes |
| 69 | * |
| 70 | * Removes slashes contained in a string or in an array |
| 71 | * |
| 72 | * @access public |
| 73 | * @param mixed string or array |
| 74 | * @return mixed string or array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | if ( ! function_exists('strip_slashes')) |
| 77 | { |
| 78 | function strip_slashes($str) |
| 79 | { |
| 80 | if (is_array($str)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | foreach ($str as $key => $val) |
| 83 | { |
| 84 | $str[$key] = strip_slashes($val); |
| 85 | } |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | $str = stripslashes($str); |
| 90 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | return $str; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // ------------------------------------------------------------------------ |
| 97 | |
| 98 | /** |
| 99 | * Strip Quotes |
| 100 | * |
| 101 | * Removes single and double quotes from a string |
| 102 | * |
| 103 | * @access public |
| 104 | * @param string |
| 105 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | if ( ! function_exists('strip_quotes')) |
| 108 | { |
| 109 | function strip_quotes($str) |
| 110 | { |
| 111 | return str_replace(array('"', "'"), '', $str); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // ------------------------------------------------------------------------ |
| 116 | |
| 117 | /** |
| 118 | * Quotes to Entities |
| 119 | * |
| 120 | * Converts single and double quotes to entities |
| 121 | * |
| 122 | * @access public |
| 123 | * @param string |
| 124 | * @return string |
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 | if ( ! function_exists('quotes_to_entities')) |
| 127 | { |
| 128 | function quotes_to_entities($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------------------------------------------ |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Reduce Double Slashes |
| 138 | * |
| 139 | * Converts double slashes in a string to a single slash, |
| 140 | * except those found in http:// |
| 141 | * |
| 142 | * http://www.some-site.com//index.php |
| 143 | * |
| 144 | * becomes: |
| 145 | * |
| 146 | * http://www.some-site.com/index.php |
| 147 | * |
| 148 | * @access public |
| 149 | * @param string |
| 150 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | if ( ! function_exists('reduce_double_slashes')) |
| 153 | { |
| 154 | function reduce_double_slashes($str) |
| 155 | { |
Pascal Kriete | 42e74bc | 2009-12-23 16:44:07 +0000 | [diff] [blame] | 156 | return preg_replace("#(^|[^:])//+#", "\\1/", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 159 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | // ------------------------------------------------------------------------ |
| 161 | |
| 162 | /** |
| 163 | * Reduce Multiples |
| 164 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 165 | * Reduces multiple instances of a particular character. Example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | * |
| 167 | * Fred, Bill,, Joe, Jimmy |
| 168 | * |
| 169 | * becomes: |
| 170 | * |
| 171 | * Fred, Bill, Joe, Jimmy |
| 172 | * |
| 173 | * @access public |
| 174 | * @param string |
| 175 | * @param string the character you wish to reduce |
| 176 | * @param bool TRUE/FALSE - whether to trim the character from the beginning/end |
| 177 | * @return string |
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 | if ( ! function_exists('reduce_multiples')) |
| 180 | { |
| 181 | function reduce_multiples($str, $character = ',', $trim = FALSE) |
| 182 | { |
| 183 | $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str); |
| 184 | |
| 185 | if ($trim === TRUE) |
| 186 | { |
| 187 | $str = trim($str, $character); |
| 188 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | return $str; |
| 191 | } |
| 192 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | // ------------------------------------------------------------------------ |
| 195 | |
| 196 | /** |
| 197 | * Create a Random String |
| 198 | * |
| 199 | * Useful for generating passwords or hashes. |
| 200 | * |
| 201 | * @access public |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 202 | * @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] | 203 | * @param integer number of characters |
| 204 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | if ( ! function_exists('random_string')) |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 207 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | function random_string($type = 'alnum', $len = 8) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | switch($type) |
| 211 | { |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 212 | case 'basic' : return mt_rand(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | case 'alnum' : |
| 215 | case 'numeric' : |
| 216 | case 'nozero' : |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 217 | case 'alpha' : |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | switch ($type) |
| 220 | { |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 221 | case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 222 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 224 | break; |
| 225 | case 'numeric' : $pool = '0123456789'; |
| 226 | break; |
| 227 | case 'nozero' : $pool = '123456789'; |
| 228 | break; |
| 229 | } |
druu | 9e2df7c | 2011-09-06 22:19:59 +0300 | [diff] [blame] | 230 | |
| 231 | $str = substr(str_shuffle(str_repeat($pool, ceil($len/strlen($pool)))),0,$len); |
| 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | return $str; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | break; |
| 235 | case 'unique' : |
| 236 | case 'md5' : |
| 237 | |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 238 | return md5(uniqid(mt_rand())); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | break; |
| 240 | case 'encrypt' : |
| 241 | case 'sha1' : |
| 242 | |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 243 | $CI =& get_instance(); |
| 244 | $CI->load->helper('security'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 472dd21 | 2010-01-23 20:03:27 +0000 | [diff] [blame] | 246 | return do_hash(uniqid(mt_rand(), TRUE), 'sha1'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
Derek Jones | 1e14192 | 2010-03-05 10:24:50 -0600 | [diff] [blame] | 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | // ------------------------------------------------------------------------ |
| 253 | |
| 254 | /** |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 255 | * Add's _1 to a string or increment the ending number to allow _2, _3, etc |
| 256 | * |
| 257 | * @param string $str required |
Phil Sturgeon | dbac73b | 2011-08-14 12:23:55 -0600 | [diff] [blame] | 258 | * @param string $separator What should the duplicate number be appended with |
| 259 | * @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] | 260 | * @return string |
| 261 | */ |
Phil Sturgeon | dbac73b | 2011-08-14 12:23:55 -0600 | [diff] [blame] | 262 | function increment_string($str, $separator = '_', $first = 1) |
Phil Sturgeon | d8ef630 | 2011-08-14 12:10:55 -0600 | [diff] [blame] | 263 | { |
| 264 | preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match); |
| 265 | |
| 266 | return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first; |
| 267 | } |
| 268 | |
| 269 | // ------------------------------------------------------------------------ |
| 270 | |
| 271 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | * Alternator |
| 273 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 274 | * Allows strings to be alternated. See docs... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | * |
| 276 | * @access public |
| 277 | * @param string (as many parameters as needed) |
| 278 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | if ( ! function_exists('alternator')) |
| 281 | { |
| 282 | function alternator() |
| 283 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | static $i; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | |
| 286 | if (func_num_args() == 0) |
| 287 | { |
| 288 | $i = 0; |
| 289 | return ''; |
| 290 | } |
| 291 | $args = func_get_args(); |
| 292 | return $args[($i++ % count($args))]; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // ------------------------------------------------------------------------ |
| 297 | |
| 298 | /** |
| 299 | * Repeater function |
| 300 | * |
| 301 | * @access public |
| 302 | * @param string |
| 303 | * @param integer number of repeats |
| 304 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | if ( ! function_exists('repeater')) |
| 307 | { |
| 308 | function repeater($data, $num = 1) |
| 309 | { |
| 310 | return (($num > 0) ? str_repeat($data, $num) : ''); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 311 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | /* End of file string_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 316 | /* Location: ./system/helpers/string_helper.php */ |