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