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