Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +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
|
| 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 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
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/string_helper.html
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 26 | */
|
| 27 |
|
| 28 | // ------------------------------------------------------------------------
|
| 29 |
|
| 30 | /**
|
| 31 | * Trim Slashes
|
| 32 | *
|
| 33 | * Removes any leading/traling slashes from a string:
|
| 34 | *
|
| 35 | * /this/that/theother/
|
| 36 | *
|
| 37 | * becomes:
|
| 38 | *
|
| 39 | * this/that/theother
|
| 40 | *
|
| 41 | * @access public
|
| 42 | * @param string
|
| 43 | * @return string
|
| 44 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 45 | if (! function_exists('trim_slashes'))
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 46 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 47 | function trim_slashes($str)
|
| 48 | {
|
| 49 | return trim($str, '/');
|
| 50 | }
|
| 51 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 52 |
|
| 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
|
Derek Allard | c04a9c4 | 2008-01-18 20:47:31 +0000 | [diff] [blame] | 62 | * @return mixed string or array
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 63 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 64 | if (! function_exists('strip_slashes'))
|
| 65 | {
|
| 66 | function strip_slashes($str)
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 67 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 68 | if (is_array($str))
|
| 69 | {
|
| 70 | foreach ($str as $key => $val)
|
| 71 | {
|
| 72 | $str[$key] = strip_slashes($val);
|
| 73 | }
|
| 74 | }
|
| 75 | else
|
| 76 | {
|
| 77 | $str = stripslashes($str);
|
| 78 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 79 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 80 | return $str;
|
| 81 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 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
|
| 94 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 95 | if (! function_exists('strip_quotes'))
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 96 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 97 | function strip_quotes($str)
|
| 98 | {
|
| 99 | return str_replace(array('"', "'"), '', $str);
|
| 100 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 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
|
| 113 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 114 | if (! function_exists('quotes_to_entities'))
|
| 115 | {
|
| 116 | function quotes_to_entities($str)
|
| 117 | {
|
| 118 | return str_replace(array("\'","\"","'",'"'), array("'",""","'","""), $str);
|
| 119 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 120 | }
|
| 121 |
|
| 122 | // ------------------------------------------------------------------------
|
| 123 | /**
|
| 124 | * Reduce Double Slashes
|
| 125 | *
|
| 126 | * Converts double slashes in a string to a single slash,
|
| 127 | * except those found in http://
|
| 128 | *
|
| 129 | * http://www.some-site.com//index.php
|
| 130 | *
|
| 131 | * becomes:
|
| 132 | *
|
| 133 | * http://www.some-site.com/index.php
|
| 134 | *
|
| 135 | * @access public
|
| 136 | * @param string
|
| 137 | * @return string
|
| 138 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 139 | if (! function_exists('reduce_double_slashes'))
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 140 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 141 | function reduce_double_slashes($str)
|
| 142 | {
|
| 143 | return preg_replace("#([^:])//+#", "\\1/", $str);
|
| 144 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 145 | }
|
| 146 |
|
| 147 | // ------------------------------------------------------------------------
|
| 148 |
|
| 149 | /**
|
| 150 | * Reduce Multiples
|
| 151 | *
|
| 152 | * Reduces multiple instances of a particular character. Example:
|
| 153 | *
|
| 154 | * Fred, Bill,, Joe, Jimmy
|
| 155 | *
|
| 156 | * becomes:
|
| 157 | *
|
| 158 | * Fred, Bill, Joe, Jimmy
|
| 159 | *
|
| 160 | * @access public
|
| 161 | * @param string
|
| 162 | * @param string the character you wish to reduce
|
| 163 | * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
|
| 164 | * @return string
|
| 165 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 166 | if (! function_exists('reduce_multiples'))
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 167 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 168 | function reduce_multiples($str, $character = ',', $trim = FALSE)
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 169 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 170 | $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
|
| 171 |
|
| 172 | if ($trim === TRUE)
|
| 173 | {
|
| 174 | $str = trim($str, $character);
|
| 175 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 176 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 177 | return $str;
|
| 178 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 179 | }
|
| 180 |
|
| 181 | // ------------------------------------------------------------------------
|
| 182 |
|
| 183 | /**
|
| 184 | * Create a Random String
|
| 185 | *
|
| 186 | * Useful for generating passwords or hashes.
|
| 187 | *
|
| 188 | * @access public
|
| 189 | * @param string type of random string. Options: alunum, numeric, nozero, unique
|
| 190 | * @param integer number of characters
|
| 191 | * @return string
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 192 | */
|
| 193 | if (! function_exists('random_string'))
|
| 194 | {
|
| 195 | function random_string($type = 'alnum', $len = 8)
|
| 196 | {
|
| 197 | switch($type)
|
| 198 | {
|
| 199 | case 'alnum' :
|
| 200 | case 'numeric' :
|
| 201 | case 'nozero' :
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 202 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 203 | switch ($type)
|
| 204 | {
|
| 205 | case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
| 206 | break;
|
| 207 | case 'numeric' : $pool = '0123456789';
|
| 208 | break;
|
| 209 | case 'nozero' : $pool = '123456789';
|
| 210 | break;
|
| 211 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 212 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 213 | $str = '';
|
| 214 | for ($i=0; $i < $len; $i++)
|
| 215 | {
|
| 216 | $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
|
| 217 | }
|
| 218 | return $str;
|
| 219 | break;
|
| 220 | case 'unique' : return md5(uniqid(mt_rand()));
|
| 221 | break;
|
| 222 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 223 | }
|
| 224 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 225 |
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 226 | // ------------------------------------------------------------------------
|
| 227 |
|
| 228 | /**
|
| 229 | * Alternator
|
| 230 | *
|
| 231 | * Allows strings to be alternated. See docs...
|
| 232 | *
|
| 233 | * @access public
|
| 234 | * @param string (as many parameters as needed)
|
| 235 | * @return string
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 236 | */
|
| 237 | if (! function_exists('alternator'))
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 238 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 239 | function alternator()
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 240 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 241 | static $i;
|
| 242 |
|
| 243 | if (func_num_args() == 0)
|
| 244 | {
|
| 245 | $i = 0;
|
| 246 | return '';
|
| 247 | }
|
| 248 | $args = func_get_args();
|
| 249 | return $args[($i++ % count($args))];
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 250 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 251 | }
|
| 252 |
|
| 253 | // ------------------------------------------------------------------------
|
| 254 |
|
| 255 | /**
|
| 256 | * Repeater function
|
| 257 | *
|
| 258 | * @access public
|
| 259 | * @param string
|
| 260 | * @param integer number of repeats
|
| 261 | * @return string
|
| 262 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 263 | if (! function_exists('repeater'))
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 264 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 265 | function repeater($data, $num = 1)
|
| 266 | {
|
| 267 | return (($num > 0) ? str_repeat($data, $num) : '');
|
| 268 | }
|
| 269 | }
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 270 |
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 271 | |
| 272 | /* End of file string_helper.php */ |
| 273 | /* Location: ./system/helpers/string_helper.php */ |