blob: 530af2f897ff1c2e25ecc130a164029ed9ca5050 [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
13This helper is loaded using the following code
14
15::
16
17 $this->load->helper('string');
18
19The following functions are available:
20
21random_string()
22===============
23
24Generates a random string based on the type and length you specify.
25Useful for creating passwords or generating random hashes.
26
27The first parameter specifies the type of string, the second parameter
28specifies the length. The following choices are available:
29
30alpha, 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 Andreev0f0b7692012-06-07 14:57:04 +030039- **sha1**: An encrypted random number based on ``sha1()``.
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41Usage example
42
43::
44
45 echo random_string('alnum', 16);
46
47increment_string()
48==================
49
50Increments a string by appending a number to it or increasing the
51number. Useful for creating "copies" or a file or duplicating database
52content which has unique titles or slugs.
53
54Usage example
55
56::
57
58 echo increment_string('file', '_'); // "file_1"
59 echo increment_string('file', '-', 2); // "file-2"
Eric Barnesbc204812011-11-27 01:12:45 -050060 echo increment_string('file_4'); // "file_5"
Derek Jones8ede1a22011-10-05 13:34:52 -050061
62alternator()
63============
64
65Allows two or more items to be alternated between, when cycling through
66a loop. Example
67
68::
69
70 for ($i = 0; $i < 10; $i++)
71 {     
72 echo alternator('string one', 'string two');
73 }
74
75You can add as many parameters as you want, and with each iteration of
76your 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
88repeater()
89==========
90
91Generates repeating copies of the data you submit. Example
92
93::
94
95 $string = "\n"; echo repeater($string, 30);
96
97The above would generate 30 newlines.
98
Andrey Andreev60826db2012-10-27 14:45:23 +030099.. note:: This function is DEPRECATED. Use the native ``str_repeat()``
100 instead.
101
Derek Jones8ede1a22011-10-05 13:34:52 -0500102reduce_double_slashes()
103=======================
104
105Converts double slashes in a string to a single slash, except those
106found 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
tiyowan5511fbf2012-03-15 21:53:07 +0400113strip_slashes()
114===============
115
116Removes 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
123You 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
134The 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 Jones8ede1a22011-10-05 13:34:52 -0500143trim_slashes()
144==============
145
146Removes 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
154reduce_multiples()
155==================
156
157Reduces multiple instances of a particular character occuring directly
158after each other. Example::
159
160 $string = "Fred, Bill,, Joe, Jimmy";
161 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
162
163The 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
169The first parameter contains the string in which you want to reduce the
170multiplies. The second parameter contains the character you want to have
171reduced. The third parameter is FALSE by default; if set to TRUE it will
172remove occurences of the character at the beginning and the end of the
173string. Example:
174
175::
176
177 $string = ",Fred, Bill,, Joe, Jimmy,";
178 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
179
180
181quotes_to_entities()
182====================
183
184Converts single and double quotes in a string to the corresponding HTML
185entities. Example
186
187::
188
189 $string = "Joe's \"dinner\"";
190 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
191
192strip_quotes()
193==============
194
195Removes single and double quotes from a string. Example::
196
197 $string = "Joe's \"dinner\"";
198 $string = strip_quotes($string); //results in "Joes dinner"
199