Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############# |
| 2 | String Helper |
| 3 | ############# |
| 4 | |
| 5 | The String Helper file contains functions that assist in working with |
| 6 | strings. |
| 7 | |
Andrey Andreev | 25e77bf | 2015-08-07 13:33:24 +0300 | [diff] [blame] | 8 | .. important:: Please note that these functions are NOT intended, nor |
| 9 | suitable to be used for any kind of security-related logic. |
| 10 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 11 | .. contents:: |
| 12 | :local: |
| 13 | |
| 14 | .. raw:: html |
| 15 | |
| 16 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | |
| 18 | Loading this Helper |
| 19 | =================== |
| 20 | |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 21 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | |
| 23 | $this->load->helper('string'); |
| 24 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 25 | Available Functions |
| 26 | =================== |
| 27 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | The following functions are available: |
| 29 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 31 | .. php:function:: random_string([$type = 'alnum'[, $len = 8]]) |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 32 | |
| 33 | :param string $type: Randomization type |
| 34 | :param int $len: Output string length |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 35 | :returns: A random string |
| 36 | :rtype: string |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 37 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 38 | Generates a random string based on the type and length you specify. |
| 39 | Useful for creating passwords or generating random hashes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 41 | The first parameter specifies the type of string, the second parameter |
| 42 | specifies the length. The following choices are available: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 44 | - **alpha**: A string with lower and uppercase letters only. |
| 45 | - **alnum**: Alpha-numeric string with lower and uppercase characters. |
| 46 | - **basic**: A random number based on ``mt_rand()``. |
| 47 | - **numeric**: Numeric string. |
| 48 | - **nozero**: Numeric string with no zeros. |
| 49 | - **md5**: An encrypted random number based on ``md5()`` (fixed length of 32). |
| 50 | - **sha1**: An encrypted random number based on ``sha1()`` (fixed length of 40). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 52 | Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 54 | echo random_string('alnum', 16); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 56 | .. note:: Usage of the *unique* and *encrypt* types is DEPRECATED. They |
| 57 | are just aliases for *md5* and *sha1* respectively. |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 58 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 59 | .. php:function:: increment_string($str[, $separator = '_'[, $first = 1]]) |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 60 | |
| 61 | :param string $str: Input string |
| 62 | :param string $separator: Separator to append a duplicate number with |
| 63 | :param int $first: Starting number |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 64 | :returns: An incremented string |
| 65 | :rtype: string |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 66 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 67 | Increments a string by appending a number to it or increasing the |
| 68 | number. Useful for creating "copies" or a file or duplicating database |
| 69 | content which has unique titles or slugs. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 71 | Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 73 | echo increment_string('file', '_'); // "file_1" |
| 74 | echo increment_string('file', '-', 2); // "file-2" |
| 75 | echo increment_string('file_4'); // "file_5" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 78 | .. php:function:: alternator($args) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 80 | :param mixed $args: A variable number of arguments |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 81 | :returns: Alternated string(s) |
| 82 | :rtype: mixed |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 83 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 84 | Allows two or more items to be alternated between, when cycling through |
| 85 | a loop. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 87 | for ($i = 0; $i < 10; $i++) |
| 88 | { |
| 89 | echo alternator('string one', 'string two'); |
| 90 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 92 | You can add as many parameters as you want, and with each iteration of |
| 93 | your loop the next item will be returned. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 95 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 97 | for ($i = 0; $i < 10; $i++) |
| 98 | { |
| 99 | echo alternator('one', 'two', 'three', 'four', 'five'); |
| 100 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 102 | .. note:: To use multiple separate calls to this function simply call the |
| 103 | function with no arguments to re-initialize. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 105 | .. php:function:: repeater($data[, $num = 1]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 106 | |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 107 | :param string $data: Input |
| 108 | :param int $num: Number of times to repeat |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 109 | :returns: Repeated string |
| 110 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 112 | Generates repeating copies of the data you submit. Example:: |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 113 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 114 | $string = "\n"; |
| 115 | echo repeater($string, 30); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 117 | The above would generate 30 newlines. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 119 | .. note:: This function is DEPRECATED. Use the native ``str_repeat()`` |
| 120 | instead. |
Andrey Andreev | 60826db | 2012-10-27 14:45:23 +0300 | [diff] [blame] | 121 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 123 | .. php:function:: reduce_double_slashes($str) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 125 | :param string $str: Input string |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 126 | :returns: A string with normalized slashes |
| 127 | :rtype: string |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 128 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 129 | Converts double slashes in a string to a single slash, except those |
Andrey Andreev | 8476056 | 2018-02-12 15:15:47 +0200 | [diff] [blame^] | 130 | found in URL protocol prefixes (e.g. \http://). |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 131 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 132 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 134 | $string = "http://example.com//index.php"; |
| 135 | echo reduce_double_slashes($string); // results in "http://example.com/index.php" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 137 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 138 | .. php:function:: strip_slashes($data) |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 139 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 140 | :param mixed $data: Input string or an array of strings |
| 141 | :returns: String(s) with stripped slashes |
| 142 | :rtype: mixed |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 143 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 144 | Removes any slashes from an array of strings. |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 145 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 146 | Example:: |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 147 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 148 | $str = array( |
| 149 | 'question' => 'Is your name O\'reilly?', |
| 150 | 'answer' => 'No, my name is O\'connor.' |
| 151 | ); |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 152 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 153 | $str = strip_slashes($str); |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 154 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 155 | The above will return the following array:: |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 156 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 157 | array( |
| 158 | 'question' => "Is your name O'reilly?", |
| 159 | 'answer' => "No, my name is O'connor." |
| 160 | ); |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 161 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 162 | .. note:: For historical reasons, this function will also accept |
| 163 | and handle string inputs. This however makes it just an |
| 164 | alias for ``stripslashes()``. |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 165 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 166 | .. php:function:: trim_slashes($str) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 168 | :param string $str: Input string |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 169 | :returns: Slash-trimmed string |
| 170 | :rtype: string |
Andrey Andreev | f7c39d6 | 2012-11-08 22:01:33 +0200 | [diff] [blame] | 171 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 172 | Removes any leading/trailing slashes from a string. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 174 | $string = "/this/that/theother/"; |
| 175 | echo trim_slashes($string); // results in this/that/theother |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 177 | .. note:: This function is DEPRECATED. Use the native ``trim()`` instead: |
| 178 | | |
| 179 | | trim($str, '/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 181 | .. php:function:: reduce_multiples($str[, $character = ''[, $trim = FALSE]]) |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 182 | |
| 183 | :param string $str: Text to search in |
| 184 | :param string $character: Character to reduce |
| 185 | :param bool $trim: Whether to also trim the specified character |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 186 | :returns: Reduced string |
| 187 | :rtype: string |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 188 | |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 189 | Reduces multiple instances of a particular character occurring directly |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 190 | after each other. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 191 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 192 | $string = "Fred, Bill,, Joe, Jimmy"; |
| 193 | $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 194 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 195 | If the third parameter is set to TRUE it will remove occurrences of the |
| 196 | character at the beginning and the end of the string. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 197 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 198 | $string = ",Fred, Bill,, Joe, Jimmy,"; |
| 199 | $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 200 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 201 | .. php:function:: quotes_to_entities($str) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 203 | :param string $str: Input string |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 204 | :returns: String with quotes converted to HTML entities |
| 205 | :rtype: string |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 206 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 207 | Converts single and double quotes in a string to the corresponding HTML |
| 208 | entities. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 209 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 210 | $string = "Joe's \"dinner\""; |
| 211 | $string = quotes_to_entities($string); //results in "Joe's "dinner"" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 212 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 213 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 214 | .. php:function:: strip_quotes($str) |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 215 | |
| 216 | :param string $str: Input string |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame] | 217 | :returns: String with quotes stripped |
| 218 | :rtype: string |
Andrey Andreev | 442682e | 2012-11-08 22:52:12 +0200 | [diff] [blame] | 219 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 220 | Removes single and double quotes from a string. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 221 | |
Derek Jones | 4b184f3 | 2013-07-19 16:41:57 -0700 | [diff] [blame] | 222 | $string = "Joe's \"dinner\""; |
Andrey Andreev | 8476056 | 2018-02-12 15:15:47 +0200 | [diff] [blame^] | 223 | $string = strip_quotes($string); //results in "Joes dinner" |