blob: 2d23fb00cd313bd4d509ef2183d243606f5a9092 [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.
39- **sha1**: An encrypted random number based on do_hash() from the
40 :doc:`security helper <security_helper>`.
41
42Usage example
43
44::
45
46 echo random_string('alnum', 16);
47
48increment_string()
49==================
50
51Increments a string by appending a number to it or increasing the
52number. Useful for creating "copies" or a file or duplicating database
53content which has unique titles or slugs.
54
55Usage example
56
57::
58
59 echo increment_string('file', '_'); // "file_1"
60 echo increment_string('file', '-', 2); // "file-2"
Eric Barnesbc204812011-11-27 01:12:45 -050061 echo increment_string('file_4'); // "file_5"
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63alternator()
64============
65
66Allows two or more items to be alternated between, when cycling through
67a loop. Example
68
69::
70
71 for ($i = 0; $i < 10; $i++)
72 {     
73 echo alternator('string one', 'string two');
74 }
75
76You can add as many parameters as you want, and with each iteration of
77your loop the next item will be returned.
78
79::
80
81 for ($i = 0; $i < 10; $i++)
82 {     
83 echo alternator('one', 'two', 'three', 'four', 'five');
84 }
85
86.. note:: To use multiple separate calls to this function simply call the
87 function with no arguments to re-initialize.
88
89repeater()
90==========
91
92Generates repeating copies of the data you submit. Example
93
94::
95
96 $string = "\n"; echo repeater($string, 30);
97
98The above would generate 30 newlines.
99
100reduce_double_slashes()
101=======================
102
103Converts double slashes in a string to a single slash, except those
104found in http://. Example
105
106::
107
108 $string = "http://example.com//index.php";
109 echo reduce_double_slashes($string); // results in "http://example.com/index.php"
110
tiyowan5511fbf2012-03-15 21:53:07 +0400111strip_slashes()
112===============
113
114Removes any slashes from a string. Example
115
116::
117
118 $str = "Is your name O\'reilly?";
119 echo strip_slashes($str); // results in Is your name O'reilly?
120
121You can also use an array. Example
122
123::
124
125 $str = array(
126 'question'  => 'Is your name O\'reilly?',
127 'answer' => 'No, my name is O\'connor.'
128 );
129
130 $str = strip_slashes($str);
131
132The above will return the following array:
133
134::
135
136 array(
137 'question'  => "Is your name O'reilly?",
138 'answer' => "No, my name is O'connor."
139 );
140
Derek Jones8ede1a22011-10-05 13:34:52 -0500141trim_slashes()
142==============
143
144Removes any leading/trailing slashes from a string. Example
145
146::
147
148 $string = "/this/that/theother/";
149 echo trim_slashes($string); // results in this/that/theother
150
151
152reduce_multiples()
153==================
154
155Reduces multiple instances of a particular character occuring directly
156after each other. Example::
157
158 $string = "Fred, Bill,, Joe, Jimmy";
159 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
160
161The function accepts the following parameters:
162
163::
164
165 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)
166
167The first parameter contains the string in which you want to reduce the
168multiplies. The second parameter contains the character you want to have
169reduced. The third parameter is FALSE by default; if set to TRUE it will
170remove occurences of the character at the beginning and the end of the
171string. Example:
172
173::
174
175 $string = ",Fred, Bill,, Joe, Jimmy,";
176 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
177
178
179quotes_to_entities()
180====================
181
182Converts single and double quotes in a string to the corresponding HTML
183entities. Example
184
185::
186
187 $string = "Joe's \"dinner\"";
188 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
189
190strip_quotes()
191==============
192
193Removes single and double quotes from a string. Example::
194
195 $string = "Joe's \"dinner\"";
196 $string = strip_quotes($string); //results in "Joes dinner"
197