blob: dc70e461a2d43aac8ce9ce22aea5f5fac077be85 [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
111trim_slashes()
112==============
113
114Removes any leading/trailing slashes from a string. Example
115
116::
117
118 $string = "/this/that/theother/";
119 echo trim_slashes($string); // results in this/that/theother
120
121
122reduce_multiples()
123==================
124
125Reduces multiple instances of a particular character occuring directly
126after each other. Example::
127
128 $string = "Fred, Bill,, Joe, Jimmy";
129 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
130
131The function accepts the following parameters:
132
133::
134
135 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)
136
137The first parameter contains the string in which you want to reduce the
138multiplies. The second parameter contains the character you want to have
139reduced. The third parameter is FALSE by default; if set to TRUE it will
140remove occurences of the character at the beginning and the end of the
141string. Example:
142
143::
144
145 $string = ",Fred, Bill,, Joe, Jimmy,";
146 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
147
148
149quotes_to_entities()
150====================
151
152Converts single and double quotes in a string to the corresponding HTML
153entities. Example
154
155::
156
157 $string = "Joe's \"dinner\"";
158 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
159
160strip_quotes()
161==============
162
163Removes single and double quotes from a string. Example::
164
165 $string = "Joe's \"dinner\"";
166 $string = strip_quotes($string); //results in "Joes dinner"
167