blob: 19500aa0dd95593cae6322ff3edd2d710012e262 [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
99reduce_double_slashes()
100=======================
101
102Converts double slashes in a string to a single slash, except those
103found in http://. Example
104
105::
106
107 $string = "http://example.com//index.php";
108 echo reduce_double_slashes($string); // results in "http://example.com/index.php"
109
tiyowan5511fbf2012-03-15 21:53:07 +0400110strip_slashes()
111===============
112
113Removes any slashes from a string. Example
114
115::
116
117 $str = "Is your name O\'reilly?";
118 echo strip_slashes($str); // results in Is your name O'reilly?
119
120You can also use an array. Example
121
122::
123
124 $str = array(
125 'question'  => 'Is your name O\'reilly?',
126 'answer' => 'No, my name is O\'connor.'
127 );
128
129 $str = strip_slashes($str);
130
131The above will return the following array:
132
133::
134
135 array(
136 'question'  => "Is your name O'reilly?",
137 'answer' => "No, my name is O'connor."
138 );
139
Derek Jones8ede1a22011-10-05 13:34:52 -0500140trim_slashes()
141==============
142
143Removes any leading/trailing slashes from a string. Example
144
145::
146
147 $string = "/this/that/theother/";
148 echo trim_slashes($string); // results in this/that/theother
149
150
151reduce_multiples()
152==================
153
154Reduces multiple instances of a particular character occuring directly
155after each other. Example::
156
157 $string = "Fred, Bill,, Joe, Jimmy";
158 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
159
160The function accepts the following parameters:
161
162::
163
164 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)
165
166The first parameter contains the string in which you want to reduce the
167multiplies. The second parameter contains the character you want to have
168reduced. The third parameter is FALSE by default; if set to TRUE it will
169remove occurences of the character at the beginning and the end of the
170string. Example:
171
172::
173
174 $string = ",Fred, Bill,, Joe, Jimmy,";
175 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
176
177
178quotes_to_entities()
179====================
180
181Converts single and double quotes in a string to the corresponding HTML
182entities. Example
183
184::
185
186 $string = "Joe's \"dinner\"";
187 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
188
189strip_quotes()
190==============
191
192Removes single and double quotes from a string. Example::
193
194 $string = "Joe's \"dinner\"";
195 $string = strip_quotes($string); //results in "Joes dinner"
196