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