blob: 6dabc60d3c1589ece3b0cf8cd113422389f598d1 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#############
2String Helper
3#############
4
5The String Helper file contains functions that assist in working with
6strings.
7
Andrey Andreev25e77bf2015-08-07 13:33:24 +03008.. important:: Please note that these functions are NOT intended, nor
9 suitable to be used for any kind of security-related logic.
10
Derek Jones4b184f32013-07-19 16:41:57 -070011.. contents::
12 :local:
13
14.. raw:: html
15
16 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050017
18Loading this Helper
19===================
20
Andrey Andreevf7c39d62012-11-08 22:01:33 +020021This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050022
23 $this->load->helper('string');
24
Derek Jones4b184f32013-07-19 16:41:57 -070025Available Functions
26===================
27
Derek Jones8ede1a22011-10-05 13:34:52 -050028The following functions are available:
29
Derek Jones8ede1a22011-10-05 13:34:52 -050030
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020031.. php:function:: random_string([$type = 'alnum'[, $len = 8]])
Andrey Andreev442682e2012-11-08 22:52:12 +020032
33 :param string $type: Randomization type
34 :param int $len: Output string length
Andrey Andreev3de130c2014-02-07 23:31:49 +020035 :returns: A random string
36 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020037
Derek Jones4b184f32013-07-19 16:41:57 -070038 Generates a random string based on the type and length you specify.
39 Useful for creating passwords or generating random hashes.
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jones4b184f32013-07-19 16:41:57 -070041 The first parameter specifies the type of string, the second parameter
42 specifies the length. The following choices are available:
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Derek Jones4b184f32013-07-19 16:41:57 -070044 - **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 Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jones4b184f32013-07-19 16:41:57 -070052 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Derek Jones4b184f32013-07-19 16:41:57 -070054 echo random_string('alnum', 16);
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Derek Jones4b184f32013-07-19 16:41:57 -070056 .. note:: Usage of the *unique* and *encrypt* types is DEPRECATED. They
57 are just aliases for *md5* and *sha1* respectively.
Andrey Andreev442682e2012-11-08 22:52:12 +020058
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020059.. php:function:: increment_string($str[, $separator = '_'[, $first = 1]])
Andrey Andreev442682e2012-11-08 22:52:12 +020060
61 :param string $str: Input string
62 :param string $separator: Separator to append a duplicate number with
63 :param int $first: Starting number
Andrey Andreev3de130c2014-02-07 23:31:49 +020064 :returns: An incremented string
65 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020066
Derek Jones4b184f32013-07-19 16:41:57 -070067 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 Jones8ede1a22011-10-05 13:34:52 -050070
Derek Jones4b184f32013-07-19 16:41:57 -070071 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Derek Jones4b184f32013-07-19 16:41:57 -070073 echo increment_string('file', '_'); // "file_1"
74 echo increment_string('file', '-', 2); // "file-2"
75 echo increment_string('file_4'); // "file_5"
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020078.. php:function:: alternator($args)
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreev442682e2012-11-08 22:52:12 +020080 :param mixed $args: A variable number of arguments
Andrey Andreev3de130c2014-02-07 23:31:49 +020081 :returns: Alternated string(s)
82 :rtype: mixed
Andrey Andreev442682e2012-11-08 22:52:12 +020083
Derek Jones4b184f32013-07-19 16:41:57 -070084 Allows two or more items to be alternated between, when cycling through
85 a loop. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Derek Jones4b184f32013-07-19 16:41:57 -070087 for ($i = 0; $i < 10; $i++)
88 {     
89 echo alternator('string one', 'string two');
90 }
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Derek Jones4b184f32013-07-19 16:41:57 -070092 You can add as many parameters as you want, and with each iteration of
93 your loop the next item will be returned.
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Derek Jones4b184f32013-07-19 16:41:57 -070095 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Derek Jones4b184f32013-07-19 16:41:57 -070097 for ($i = 0; $i < 10; $i++)
98 {     
99 echo alternator('one', 'two', 'three', 'four', 'five');
100 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
Derek Jones4b184f32013-07-19 16:41:57 -0700102 .. note:: To use multiple separate calls to this function simply call the
103 function with no arguments to re-initialize.
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200105.. php:function:: repeater($data[, $num = 1])
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200107 :param string $data: Input
108 :param int $num: Number of times to repeat
Andrey Andreev3de130c2014-02-07 23:31:49 +0200109 :returns: Repeated string
110 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Derek Jones4b184f32013-07-19 16:41:57 -0700112 Generates repeating copies of the data you submit. Example::
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200113
Derek Jones4b184f32013-07-19 16:41:57 -0700114 $string = "\n";
115 echo repeater($string, 30);
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jones4b184f32013-07-19 16:41:57 -0700117 The above would generate 30 newlines.
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Derek Jones4b184f32013-07-19 16:41:57 -0700119 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
120 instead.
Andrey Andreev60826db2012-10-27 14:45:23 +0300121
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200123.. php:function:: reduce_double_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Andrey Andreev442682e2012-11-08 22:52:12 +0200125 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200126 :returns: A string with normalized slashes
127 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200128
Derek Jones4b184f32013-07-19 16:41:57 -0700129 Converts double slashes in a string to a single slash, except those
Andrey Andreev84760562018-02-12 15:15:47 +0200130 found in URL protocol prefixes (e.g. \http://).
Andrey Andreev442682e2012-11-08 22:52:12 +0200131
Derek Jones4b184f32013-07-19 16:41:57 -0700132 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Derek Jones4b184f32013-07-19 16:41:57 -0700134 $string = "http://example.com//index.php";
135 echo reduce_double_slashes($string); // results in "http://example.com/index.php"
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
tiyowan5511fbf2012-03-15 21:53:07 +0400137
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200138.. php:function:: strip_slashes($data)
tiyowan5511fbf2012-03-15 21:53:07 +0400139
Andrey Andreev3de130c2014-02-07 23:31:49 +0200140 :param mixed $data: Input string or an array of strings
141 :returns: String(s) with stripped slashes
142 :rtype: mixed
tiyowan5511fbf2012-03-15 21:53:07 +0400143
Andrey Andreev3de130c2014-02-07 23:31:49 +0200144 Removes any slashes from an array of strings.
tiyowan5511fbf2012-03-15 21:53:07 +0400145
Derek Jones4b184f32013-07-19 16:41:57 -0700146 Example::
Derek Jonesb8c283a2013-07-19 16:02:53 -0700147
Derek Jones4b184f32013-07-19 16:41:57 -0700148 $str = array(
149 'question'  => 'Is your name O\'reilly?',
150 'answer' => 'No, my name is O\'connor.'
151 );
Derek Jonesb8c283a2013-07-19 16:02:53 -0700152
Derek Jones4b184f32013-07-19 16:41:57 -0700153 $str = strip_slashes($str);
Derek Jonesb8c283a2013-07-19 16:02:53 -0700154
Derek Jones4b184f32013-07-19 16:41:57 -0700155 The above will return the following array::
tiyowan5511fbf2012-03-15 21:53:07 +0400156
Derek Jones4b184f32013-07-19 16:41:57 -0700157 array(
158 'question'  => "Is your name O'reilly?",
159 'answer' => "No, my name is O'connor."
160 );
tiyowan5511fbf2012-03-15 21:53:07 +0400161
Derek Jones4b184f32013-07-19 16:41:57 -0700162 .. 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 Andreev442682e2012-11-08 22:52:12 +0200165
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200166.. php:function:: trim_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200168 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200169 :returns: Slash-trimmed string
170 :rtype: string
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200171
Derek Jones4b184f32013-07-19 16:41:57 -0700172 Removes any leading/trailing slashes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Derek Jones4b184f32013-07-19 16:41:57 -0700174 $string = "/this/that/theother/";
175 echo trim_slashes($string); // results in this/that/theother
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Derek Jones4b184f32013-07-19 16:41:57 -0700177 .. note:: This function is DEPRECATED. Use the native ``trim()`` instead:
178 |
179 | trim($str, '/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200181.. php:function:: reduce_multiples($str[, $character = ''[, $trim = FALSE]])
Andrey Andreev442682e2012-11-08 22:52:12 +0200182
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 Andreev3de130c2014-02-07 23:31:49 +0200186 :returns: Reduced string
187 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200188
Andrey Andreev71d8f722017-01-17 12:01:00 +0200189 Reduces multiple instances of a particular character occurring directly
Derek Jones4b184f32013-07-19 16:41:57 -0700190 after each other. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
Derek Jones4b184f32013-07-19 16:41:57 -0700192 $string = "Fred, Bill,, Joe, Jimmy";
193 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Derek Jones4b184f32013-07-19 16:41:57 -0700195 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 Jones8ede1a22011-10-05 13:34:52 -0500197
Derek Jones4b184f32013-07-19 16:41:57 -0700198 $string = ",Fred, Bill,, Joe, Jimmy,";
199 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200201.. php:function:: quotes_to_entities($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Andrey Andreev442682e2012-11-08 22:52:12 +0200203 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200204 :returns: String with quotes converted to HTML entities
205 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200206
Derek Jones4b184f32013-07-19 16:41:57 -0700207 Converts single and double quotes in a string to the corresponding HTML
208 entities. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
Derek Jones4b184f32013-07-19 16:41:57 -0700210 $string = "Joe's \"dinner\"";
211 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200214.. php:function:: strip_quotes($str)
Andrey Andreev442682e2012-11-08 22:52:12 +0200215
216 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200217 :returns: String with quotes stripped
218 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200219
Derek Jones4b184f32013-07-19 16:41:57 -0700220 Removes single and double quotes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
Derek Jones4b184f32013-07-19 16:41:57 -0700222 $string = "Joe's \"dinner\"";
Andrey Andreev84760562018-02-12 15:15:47 +0200223 $string = strip_quotes($string); //results in "Joes dinner"