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