summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/helpers/string_helper.rst
blob: 530af2f897ff1c2e25ecc130a164029ed9ca5050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#############
String Helper
#############

The String Helper file contains functions that assist in working with
strings.

.. contents:: Page Contents

Loading this Helper
===================

This helper is loaded using the following code

::

	$this->load->helper('string');

The following functions are available:

random_string()
===============

Generates a random string based on the type and length you specify.
Useful for creating passwords or generating random hashes.

The first parameter specifies the type of string, the second parameter
specifies the length. The following choices are available:

alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1

-  **alpha**: A string with lower and uppercase letters only.
-  **alnum**: Alpha-numeric string with lower and uppercase characters.
-  **numeric**: Numeric string.
-  **nozero**: Numeric string with no zeros.
-  **unique**: Encrypted with MD5 and uniqid(). Note: The length
   parameter is not available for this type. Returns a fixed length 32
   character string.
-  **sha1**: An encrypted random number based on ``sha1()``.

Usage example

::

	echo random_string('alnum', 16);

increment_string()
==================

Increments a string by appending a number to it or increasing the
number. Useful for creating "copies" or a file or duplicating database
content which has unique titles or slugs.

Usage example

::

	echo increment_string('file', '_'); // "file_1"
	echo increment_string('file', '-', 2); // "file-2"
	echo increment_string('file_4'); // "file_5"

alternator()
============

Allows two or more items to be alternated between, when cycling through
a loop. Example

::

	for ($i = 0; $i < 10; $i++)
	{     
		echo alternator('string one', 'string two');
	}

You can add as many parameters as you want, and with each iteration of
your loop the next item will be returned.

::

	for ($i = 0; $i < 10; $i++)
	{     
		echo alternator('one', 'two', 'three', 'four', 'five');
	}

.. note:: To use multiple separate calls to this function simply call the
	function with no arguments to re-initialize.

repeater()
==========

Generates repeating copies of the data you submit. Example

::

	$string = "\n"; echo repeater($string, 30);

The above would generate 30 newlines.

.. note:: This function is DEPRECATED. Use the native ``str_repeat()``
	instead.

reduce_double_slashes()
=======================

Converts double slashes in a string to a single slash, except those
found in http://. Example

::

	$string = "http://example.com//index.php";
	echo reduce_double_slashes($string); // results in "http://example.com/index.php"

strip_slashes()
===============

Removes any slashes from a string. Example

::

	$str = "Is your name O\'reilly?";
	echo strip_slashes($str); // results in Is your name O'reilly?

You can also use an array. Example

::
	
	$str = array(
		'question'  => 'Is your name O\'reilly?',
		'answer' => 'No, my name is O\'connor.'
	);
	
	$str = strip_slashes($str);
	
The above will return the following array:

::

	array(
		'question'  => "Is your name O'reilly?",
		'answer' => "No, my name is O'connor."
	);

trim_slashes()
==============

Removes any leading/trailing slashes from a string. Example

::

	$string = "/this/that/theother/";
	echo trim_slashes($string); // results in this/that/theother


reduce_multiples()
==================

Reduces multiple instances of a particular character occuring directly
after each other. Example::

	$string = "Fred, Bill,, Joe, Jimmy";
	$string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"

The function accepts the following parameters:

::

	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)

The first parameter contains the string in which you want to reduce the
multiplies. The second parameter contains the character you want to have
reduced. The third parameter is FALSE by default; if set to TRUE it will
remove occurences of the character at the beginning and the end of the
string. Example:

::

	$string = ",Fred, Bill,, Joe, Jimmy,";
	$string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"


quotes_to_entities()
====================

Converts single and double quotes in a string to the corresponding HTML
entities. Example

::

	$string = "Joe's \"dinner\"";
	$string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"

strip_quotes()
==============

Removes single and double quotes from a string. Example::

	$string = "Joe's \"dinner\"";
	$string = strip_quotes($string); //results in "Joes dinner"