blob: 1ca654ea6464e8c05c3277f028091b1c2aed3c83 [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
Derek Jones4b184f32013-07-19 16:41:57 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
Andrey Andreevf7c39d62012-11-08 22:01:33 +020018This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050019
20 $this->load->helper('string');
21
Derek Jones4b184f32013-07-19 16:41:57 -070022Available Functions
23===================
24
Derek Jones8ede1a22011-10-05 13:34:52 -050025The following functions are available:
26
Derek Jones8ede1a22011-10-05 13:34:52 -050027
Derek Jones4b184f32013-07-19 16:41:57 -070028.. function:: random_string([$type = 'alnum'[, $len = 8]])
Andrey Andreev442682e2012-11-08 22:52:12 +020029
30 :param string $type: Randomization type
31 :param int $len: Output string length
32 :returns: string
33
Derek Jones4b184f32013-07-19 16:41:57 -070034 Generates a random string based on the type and length you specify.
35 Useful for creating passwords or generating random hashes.
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Derek Jones4b184f32013-07-19 16:41:57 -070037 The first parameter specifies the type of string, the second parameter
38 specifies the length. The following choices are available:
Derek Jones8ede1a22011-10-05 13:34:52 -050039
Derek Jones4b184f32013-07-19 16:41:57 -070040 - **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 Jones8ede1a22011-10-05 13:34:52 -050047
Derek Jones4b184f32013-07-19 16:41:57 -070048 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Derek Jones4b184f32013-07-19 16:41:57 -070050 echo random_string('alnum', 16);
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jones4b184f32013-07-19 16:41:57 -070052 .. note:: Usage of the *unique* and *encrypt* types is DEPRECATED. They
53 are just aliases for *md5* and *sha1* respectively.
Andrey Andreev442682e2012-11-08 22:52:12 +020054
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Derek Jones4b184f32013-07-19 16:41:57 -070056.. function:: increment_string($str[, $separator = '_'[, $first = 1]])
Andrey Andreev442682e2012-11-08 22:52:12 +020057
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 Jones4b184f32013-07-19 16:41:57 -070063 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 Jones8ede1a22011-10-05 13:34:52 -050066
Derek Jones4b184f32013-07-19 16:41:57 -070067 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Derek Jones4b184f32013-07-19 16:41:57 -070069 echo increment_string('file', '_'); // "file_1"
70 echo increment_string('file', '-', 2); // "file-2"
71 echo increment_string('file_4'); // "file_5"
Derek Jones8ede1a22011-10-05 13:34:52 -050072
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Derek Jonesb8c283a2013-07-19 16:02:53 -070074.. function:: alternator($args)
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Andrey Andreev442682e2012-11-08 22:52:12 +020076 :param mixed $args: A variable number of arguments
77 :returns: mixed
78
Derek Jones4b184f32013-07-19 16:41:57 -070079 Allows two or more items to be alternated between, when cycling through
80 a loop. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050081
Derek Jones4b184f32013-07-19 16:41:57 -070082 for ($i = 0; $i < 10; $i++)
83 {     
84 echo alternator('string one', 'string two');
85 }
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Derek Jones4b184f32013-07-19 16:41:57 -070087 You can add as many parameters as you want, and with each iteration of
88 your loop the next item will be returned.
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Derek Jones4b184f32013-07-19 16:41:57 -070090 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Derek Jones4b184f32013-07-19 16:41:57 -070092 for ($i = 0; $i < 10; $i++)
93 {     
94 echo alternator('one', 'two', 'three', 'four', 'five');
95 }
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Derek Jones4b184f32013-07-19 16:41:57 -070097 .. note:: To use multiple separate calls to this function simply call the
98 function with no arguments to re-initialize.
Derek Jones8ede1a22011-10-05 13:34:52 -050099
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Derek Jones4b184f32013-07-19 16:41:57 -0700101.. function:: repeater($data[, $num = 1])
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200103 :param string $data: Input
104 :param int $num: Number of times to repeat
105 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Derek Jones4b184f32013-07-19 16:41:57 -0700107 Generates repeating copies of the data you submit. Example::
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200108
Derek Jones4b184f32013-07-19 16:41:57 -0700109 $string = "\n";
110 echo repeater($string, 30);
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Derek Jones4b184f32013-07-19 16:41:57 -0700112 The above would generate 30 newlines.
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Derek Jones4b184f32013-07-19 16:41:57 -0700114 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
115 instead.
Andrey Andreev60826db2012-10-27 14:45:23 +0300116
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Derek Jonesb8c283a2013-07-19 16:02:53 -0700118.. function:: reduce_double_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev442682e2012-11-08 22:52:12 +0200120 :param string $str: Input string
121 :returns: string
122
Derek Jones4b184f32013-07-19 16:41:57 -0700123 Converts double slashes in a string to a single slash, except those
124 found in URL protocol prefixes (e.g. http://).
Andrey Andreev442682e2012-11-08 22:52:12 +0200125
Derek Jones4b184f32013-07-19 16:41:57 -0700126 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Derek Jones4b184f32013-07-19 16:41:57 -0700128 $string = "http://example.com//index.php";
129 echo reduce_double_slashes($string); // results in "http://example.com/index.php"
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
tiyowan5511fbf2012-03-15 21:53:07 +0400131
Derek Jonesb8c283a2013-07-19 16:02:53 -0700132.. function:: strip_slashes($data)
tiyowan5511fbf2012-03-15 21:53:07 +0400133
Andrey Andreev442682e2012-11-08 22:52:12 +0200134 :param array $data: Input
135 :returns: array
tiyowan5511fbf2012-03-15 21:53:07 +0400136
Andrey Andreev442682e2012-11-08 22:52:12 +0200137Removes any slashes from an array of strings.
tiyowan5511fbf2012-03-15 21:53:07 +0400138
Derek Jones4b184f32013-07-19 16:41:57 -0700139 Example::
Derek Jonesb8c283a2013-07-19 16:02:53 -0700140
Derek Jones4b184f32013-07-19 16:41:57 -0700141 $str = array(
142 'question'  => 'Is your name O\'reilly?',
143 'answer' => 'No, my name is O\'connor.'
144 );
Derek Jonesb8c283a2013-07-19 16:02:53 -0700145
Derek Jones4b184f32013-07-19 16:41:57 -0700146 $str = strip_slashes($str);
Derek Jonesb8c283a2013-07-19 16:02:53 -0700147
Derek Jones4b184f32013-07-19 16:41:57 -0700148 The above will return the following array::
tiyowan5511fbf2012-03-15 21:53:07 +0400149
Derek Jones4b184f32013-07-19 16:41:57 -0700150 array(
151 'question'  => "Is your name O'reilly?",
152 'answer' => "No, my name is O'connor."
153 );
tiyowan5511fbf2012-03-15 21:53:07 +0400154
Derek Jones4b184f32013-07-19 16:41:57 -0700155 .. 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 Andreev442682e2012-11-08 22:52:12 +0200158
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Derek Jonesb8c283a2013-07-19 16:02:53 -0700160.. function:: trim_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200162 :param string $str: Input string
163 :returns: string
164
Derek Jones4b184f32013-07-19 16:41:57 -0700165 Removes any leading/trailing slashes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Derek Jones4b184f32013-07-19 16:41:57 -0700167 $string = "/this/that/theother/";
168 echo trim_slashes($string); // results in this/that/theother
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Derek Jones4b184f32013-07-19 16:41:57 -0700170 .. note:: This function is DEPRECATED. Use the native ``trim()`` instead:
171 |
172 | trim($str, '/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Derek Jones4b184f32013-07-19 16:41:57 -0700175.. function:: reduce_multiples($str[, $character = ''[, $trim = FALSE]])
Andrey Andreev442682e2012-11-08 22:52:12 +0200176
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 Jones4b184f32013-07-19 16:41:57 -0700182 Reduces multiple instances of a particular character occuring directly
183 after each other. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Derek Jones4b184f32013-07-19 16:41:57 -0700185 $string = "Fred, Bill,, Joe, Jimmy";
186 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Derek Jones4b184f32013-07-19 16:41:57 -0700188 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 Jones8ede1a22011-10-05 13:34:52 -0500190
Derek Jones4b184f32013-07-19 16:41:57 -0700191 $string = ",Fred, Bill,, Joe, Jimmy,";
192 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Derek Jonesb8c283a2013-07-19 16:02:53 -0700195.. function:: quotes_to_entities($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
Andrey Andreev442682e2012-11-08 22:52:12 +0200197 :param string $str: Input string
198 :returns: string
199
Derek Jones4b184f32013-07-19 16:41:57 -0700200 Converts single and double quotes in a string to the corresponding HTML
201 entities. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
Derek Jones4b184f32013-07-19 16:41:57 -0700203 $string = "Joe's \"dinner\"";
204 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
Derek Jones8ede1a22011-10-05 13:34:52 -0500205
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Derek Jonesb8c283a2013-07-19 16:02:53 -0700207.. function:: strip_quotes($str)
Andrey Andreev442682e2012-11-08 22:52:12 +0200208
209 :param string $str: Input string
210 :returns: string
211
Derek Jones4b184f32013-07-19 16:41:57 -0700212 Removes single and double quotes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Derek Jones4b184f32013-07-19 16:41:57 -0700214 $string = "Joe's \"dinner\"";
215 $string = strip_quotes($string); //results in "Joes dinner"