blob: d0d302476ed973b19535402cc515601f2f1e62fd [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
8.. contents:: Page Contents
9
10Loading this Helper
11===================
12
Andrey Andreevf7c39d62012-11-08 22:01:33 +020013This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15 $this->load->helper('string');
16
17The following functions are available:
18
19random_string()
20===============
21
Andrey Andreev442682e2012-11-08 22:52:12 +020022.. 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 Jones8ede1a22011-10-05 13:34:52 -050028Generates a random string based on the type and length you specify.
29Useful for creating passwords or generating random hashes.
30
31The first parameter specifies the type of string, the second parameter
32specifies the length. The following choices are available:
33
Derek Jones8ede1a22011-10-05 13:34:52 -050034- **alpha**: A string with lower and uppercase letters only.
35- **alnum**: Alpha-numeric string with lower and uppercase characters.
Andrey Andreev442682e2012-11-08 22:52:12 +020036- **basic**: A random number based on ``mt_rand()``.
Derek Jones8ede1a22011-10-05 13:34:52 -050037- **numeric**: Numeric string.
38- **nozero**: Numeric string with no zeros.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020039- **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 Jones8ede1a22011-10-05 13:34:52 -050041
Andrey Andreev442682e2012-11-08 22:52:12 +020042Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050043
44 echo random_string('alnum', 16);
45
Andrey Andreev442682e2012-11-08 22:52:12 +020046.. note:: Usage of the *unique* and *encrypt* types is DEPRECATED. They
47 are just aliases for *md5* and *sha1* respectively.
48
Derek Jones8ede1a22011-10-05 13:34:52 -050049increment_string()
50==================
51
Andrey Andreev442682e2012-11-08 22:52:12 +020052.. 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 Jones8ede1a22011-10-05 13:34:52 -050059Increments a string by appending a number to it or increasing the
60number. Useful for creating "copies" or a file or duplicating database
61content which has unique titles or slugs.
62
Andrey Andreev442682e2012-11-08 22:52:12 +020063Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65 echo increment_string('file', '_'); // "file_1"
66 echo increment_string('file', '-', 2); // "file-2"
Eric Barnesbc204812011-11-27 01:12:45 -050067 echo increment_string('file_4'); // "file_5"
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69alternator()
70============
71
Andrey Andreev442682e2012-11-08 22:52:12 +020072.. php:function:: alternator($args)
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Andrey Andreev442682e2012-11-08 22:52:12 +020074 :param mixed $args: A variable number of arguments
75 :returns: mixed
76
77Allows two or more items to be alternated between, when cycling through
78a loop. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050079
80 for ($i = 0; $i < 10; $i++)
81 {     
82 echo alternator('string one', 'string two');
83 }
84
85You can add as many parameters as you want, and with each iteration of
86your 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
98repeater()
99==========
100
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200101.. php: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
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200107Generates repeating copies of the data you submit. Example::
108
109 $string = "\n";
110 echo repeater($string, 30);
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
112The above would generate 30 newlines.
113
Andrey Andreev60826db2012-10-27 14:45:23 +0300114.. note:: This function is DEPRECATED. Use the native ``str_repeat()``
115 instead.
116
Derek Jones8ede1a22011-10-05 13:34:52 -0500117reduce_double_slashes()
118=======================
119
Andrey Andreev442682e2012-11-08 22:52:12 +0200120.. php:function:: reduce_double_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev442682e2012-11-08 22:52:12 +0200122 :param string $str: Input string
123 :returns: string
124
125Converts double slashes in a string to a single slash, except those
126found in URL protocol prefixes (e.g. http://).
127
128Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130 $string = "http://example.com//index.php";
131 echo reduce_double_slashes($string); // results in "http://example.com/index.php"
132
tiyowan5511fbf2012-03-15 21:53:07 +0400133strip_slashes()
134===============
135
Andrey Andreev442682e2012-11-08 22:52:12 +0200136.. php:function:: strip_slashes($data)
tiyowan5511fbf2012-03-15 21:53:07 +0400137
Andrey Andreev442682e2012-11-08 22:52:12 +0200138 :param array $data: Input
139 :returns: array
tiyowan5511fbf2012-03-15 21:53:07 +0400140
Andrey Andreev442682e2012-11-08 22:52:12 +0200141Removes any slashes from an array of strings.
tiyowan5511fbf2012-03-15 21:53:07 +0400142
Andrey Andreev442682e2012-11-08 22:52:12 +0200143Example::
tiyowan5511fbf2012-03-15 21:53:07 +0400144
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 Andreev442682e2012-11-08 22:52:12 +0200152The above will return the following array::
tiyowan5511fbf2012-03-15 21:53:07 +0400153
154 array(
155 'question'  => "Is your name O'reilly?",
156 'answer' => "No, my name is O'connor."
157 );
158
Andrey Andreev442682e2012-11-08 22:52:12 +0200159.. 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 Jones8ede1a22011-10-05 13:34:52 -0500163trim_slashes()
164==============
165
Andrey Andreevf7c39d62012-11-08 22:01:33 +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
169 :returns: string
170
171Removes any leading/trailing slashes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173 $string = "/this/that/theother/";
174 echo trim_slashes($string); // results in this/that/theother
175
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200176.. note:: This function is DEPRECATED. Use the native ``trim()`` instead:
177 |
178 | trim($str, '/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
180reduce_multiples()
181==================
182
Andrey Andreev442682e2012-11-08 22:52:12 +0200183.. 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 Jones8ede1a22011-10-05 13:34:52 -0500190Reduces multiple instances of a particular character occuring directly
191after each other. Example::
192
193 $string = "Fred, Bill,, Joe, Jimmy";
194 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
195
Andrey Andreev442682e2012-11-08 22:52:12 +0200196If the third parameter is set to TRUE it will remove occurences of the
197character at the beginning and the end of the string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199 $string = ",Fred, Bill,, Joe, Jimmy,";
200 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
201
Derek Jones8ede1a22011-10-05 13:34:52 -0500202quotes_to_entities()
203====================
204
Andrey Andreev442682e2012-11-08 22:52:12 +0200205.. php:function:: quotes_to_entities($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Andrey Andreev442682e2012-11-08 22:52:12 +0200207 :param string $str: Input string
208 :returns: string
209
210Converts single and double quotes in a string to the corresponding HTML
211entities. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213 $string = "Joe's \"dinner\"";
214 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
215
216strip_quotes()
217==============
218
Andrey Andreev442682e2012-11-08 22:52:12 +0200219.. php:function:: strip_quotes($str)
220
221 :param string $str: Input string
222 :returns: string
223
Derek Jones8ede1a22011-10-05 13:34:52 -0500224Removes single and double quotes from a string. Example::
225
226 $string = "Joe's \"dinner\"";
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200227 $string = strip_quotes($string); //results in "Joes dinner"