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 | |
| 13 | This helper is loaded using the following code |
| 14 | |
| 15 | :: |
| 16 | |
| 17 | $this->load->helper('string'); |
| 18 | |
| 19 | The following functions are available: |
| 20 | |
| 21 | random_string() |
| 22 | =============== |
| 23 | |
| 24 | Generates a random string based on the type and length you specify. |
| 25 | Useful for creating passwords or generating random hashes. |
| 26 | |
| 27 | The first parameter specifies the type of string, the second parameter |
| 28 | specifies the length. The following choices are available: |
| 29 | |
| 30 | alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1 |
| 31 | |
| 32 | - **alpha**: A string with lower and uppercase letters only. |
| 33 | - **alnum**: Alpha-numeric string with lower and uppercase characters. |
| 34 | - **numeric**: Numeric string. |
| 35 | - **nozero**: Numeric string with no zeros. |
| 36 | - **unique**: Encrypted with MD5 and uniqid(). Note: The length |
| 37 | parameter is not available for this type. Returns a fixed length 32 |
| 38 | character string. |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 39 | - **sha1**: An encrypted random number based on ``sha1()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
| 41 | Usage example |
| 42 | |
| 43 | :: |
| 44 | |
| 45 | echo random_string('alnum', 16); |
| 46 | |
| 47 | increment_string() |
| 48 | ================== |
| 49 | |
| 50 | Increments a string by appending a number to it or increasing the |
| 51 | number. Useful for creating "copies" or a file or duplicating database |
| 52 | content which has unique titles or slugs. |
| 53 | |
| 54 | Usage example |
| 55 | |
| 56 | :: |
| 57 | |
| 58 | echo increment_string('file', '_'); // "file_1" |
| 59 | echo increment_string('file', '-', 2); // "file-2" |
Eric Barnes | bc20481 | 2011-11-27 01:12:45 -0500 | [diff] [blame] | 60 | echo increment_string('file_4'); // "file_5" |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | |
| 62 | alternator() |
| 63 | ============ |
| 64 | |
| 65 | Allows two or more items to be alternated between, when cycling through |
| 66 | a loop. Example |
| 67 | |
| 68 | :: |
| 69 | |
| 70 | for ($i = 0; $i < 10; $i++) |
| 71 | { |
| 72 | echo alternator('string one', 'string two'); |
| 73 | } |
| 74 | |
| 75 | You can add as many parameters as you want, and with each iteration of |
| 76 | your loop the next item will be returned. |
| 77 | |
| 78 | :: |
| 79 | |
| 80 | for ($i = 0; $i < 10; $i++) |
| 81 | { |
| 82 | echo alternator('one', 'two', 'three', 'four', 'five'); |
| 83 | } |
| 84 | |
| 85 | .. note:: To use multiple separate calls to this function simply call the |
| 86 | function with no arguments to re-initialize. |
| 87 | |
| 88 | repeater() |
| 89 | ========== |
| 90 | |
| 91 | Generates repeating copies of the data you submit. Example |
| 92 | |
| 93 | :: |
| 94 | |
| 95 | $string = "\n"; echo repeater($string, 30); |
| 96 | |
| 97 | The above would generate 30 newlines. |
| 98 | |
Andrey Andreev | 60826db | 2012-10-27 14:45:23 +0300 | [diff] [blame] | 99 | .. note:: This function is DEPRECATED. Use the native ``str_repeat()`` |
| 100 | instead. |
| 101 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | reduce_double_slashes() |
| 103 | ======================= |
| 104 | |
| 105 | Converts double slashes in a string to a single slash, except those |
| 106 | found in http://. Example |
| 107 | |
| 108 | :: |
| 109 | |
| 110 | $string = "http://example.com//index.php"; |
| 111 | echo reduce_double_slashes($string); // results in "http://example.com/index.php" |
| 112 | |
tiyowan | 5511fbf | 2012-03-15 21:53:07 +0400 | [diff] [blame] | 113 | strip_slashes() |
| 114 | =============== |
| 115 | |
| 116 | Removes any slashes from a string. Example |
| 117 | |
| 118 | :: |
| 119 | |
| 120 | $str = "Is your name O\'reilly?"; |
| 121 | echo strip_slashes($str); // results in Is your name O'reilly? |
| 122 | |
| 123 | You can also use an array. Example |
| 124 | |
| 125 | :: |
| 126 | |
| 127 | $str = array( |
| 128 | 'question' => 'Is your name O\'reilly?', |
| 129 | 'answer' => 'No, my name is O\'connor.' |
| 130 | ); |
| 131 | |
| 132 | $str = strip_slashes($str); |
| 133 | |
| 134 | The above will return the following array: |
| 135 | |
| 136 | :: |
| 137 | |
| 138 | array( |
| 139 | 'question' => "Is your name O'reilly?", |
| 140 | 'answer' => "No, my name is O'connor." |
| 141 | ); |
| 142 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | trim_slashes() |
| 144 | ============== |
| 145 | |
| 146 | Removes any leading/trailing slashes from a string. Example |
| 147 | |
| 148 | :: |
| 149 | |
| 150 | $string = "/this/that/theother/"; |
| 151 | echo trim_slashes($string); // results in this/that/theother |
| 152 | |
| 153 | |
| 154 | reduce_multiples() |
| 155 | ================== |
| 156 | |
| 157 | Reduces multiple instances of a particular character occuring directly |
| 158 | after each other. Example:: |
| 159 | |
| 160 | $string = "Fred, Bill,, Joe, Jimmy"; |
| 161 | $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy" |
| 162 | |
| 163 | The function accepts the following parameters: |
| 164 | |
| 165 | :: |
| 166 | |
| 167 | reduce_multiples(string: text to search in, string: character to reduce, boolean: whether to remove the character from the front and end of the string) |
| 168 | |
| 169 | The first parameter contains the string in which you want to reduce the |
| 170 | multiplies. The second parameter contains the character you want to have |
| 171 | reduced. The third parameter is FALSE by default; if set to TRUE it will |
| 172 | remove occurences of the character at the beginning and the end of the |
| 173 | string. Example: |
| 174 | |
| 175 | :: |
| 176 | |
| 177 | $string = ",Fred, Bill,, Joe, Jimmy,"; |
| 178 | $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy" |
| 179 | |
| 180 | |
| 181 | quotes_to_entities() |
| 182 | ==================== |
| 183 | |
| 184 | Converts single and double quotes in a string to the corresponding HTML |
| 185 | entities. Example |
| 186 | |
| 187 | :: |
| 188 | |
| 189 | $string = "Joe's \"dinner\""; |
| 190 | $string = quotes_to_entities($string); //results in "Joe's "dinner"" |
| 191 | |
| 192 | strip_quotes() |
| 193 | ============== |
| 194 | |
| 195 | Removes single and double quotes from a string. Example:: |
| 196 | |
| 197 | $string = "Joe's \"dinner\""; |
| 198 | $string = strip_quotes($string); //results in "Joes dinner" |
| 199 | |