blob: a1acb215c19b59e89a5184931f0441d49daaed51 [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
Derek Jones4b184f32013-07-19 16:41:57 -07008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050014
15Loading this Helper
16===================
17
Andrey Andreevf7c39d62012-11-08 22:01:33 +020018This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050019
20 $this->load->helper('string');
21
Derek Jones4b184f32013-07-19 16:41:57 -070022Available Functions
23===================
24
Derek Jones8ede1a22011-10-05 13:34:52 -050025The following functions are available:
26
Derek Jones8ede1a22011-10-05 13:34:52 -050027
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020028.. php:function:: random_string([$type = 'alnum'[, $len = 8]])
Andrey Andreev442682e2012-11-08 22:52:12 +020029
30 :param string $type: Randomization type
31 :param int $len: Output string length
Andrey Andreev3de130c2014-02-07 23:31:49 +020032 :returns: A random string
33 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020034
Derek Jones4b184f32013-07-19 16:41:57 -070035 Generates a random string based on the type and length you specify.
36 Useful for creating passwords or generating random hashes.
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Derek Jones4b184f32013-07-19 16:41:57 -070038 The first parameter specifies the type of string, the second parameter
39 specifies the length. The following choices are available:
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jones4b184f32013-07-19 16:41:57 -070041 - **alpha**: A string with lower and uppercase letters only.
42 - **alnum**: Alpha-numeric string with lower and uppercase characters.
43 - **basic**: A random number based on ``mt_rand()``.
44 - **numeric**: Numeric string.
45 - **nozero**: Numeric string with no zeros.
46 - **md5**: An encrypted random number based on ``md5()`` (fixed length of 32).
47 - **sha1**: An encrypted random number based on ``sha1()`` (fixed length of 40).
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Derek Jones4b184f32013-07-19 16:41:57 -070049 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050050
Derek Jones4b184f32013-07-19 16:41:57 -070051 echo random_string('alnum', 16);
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Derek Jones4b184f32013-07-19 16:41:57 -070053 .. note:: Usage of the *unique* and *encrypt* types is DEPRECATED. They
54 are just aliases for *md5* and *sha1* respectively.
Andrey Andreev442682e2012-11-08 22:52:12 +020055
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020056.. php:function:: increment_string($str[, $separator = '_'[, $first = 1]])
Andrey Andreev442682e2012-11-08 22:52:12 +020057
58 :param string $str: Input string
59 :param string $separator: Separator to append a duplicate number with
60 :param int $first: Starting number
Andrey Andreev3de130c2014-02-07 23:31:49 +020061 :returns: An incremented string
62 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +020063
Derek Jones4b184f32013-07-19 16:41:57 -070064 Increments a string by appending a number to it or increasing the
65 number. Useful for creating "copies" or a file or duplicating database
66 content which has unique titles or slugs.
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Derek Jones4b184f32013-07-19 16:41:57 -070068 Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050069
Derek Jones4b184f32013-07-19 16:41:57 -070070 echo increment_string('file', '_'); // "file_1"
71 echo increment_string('file', '-', 2); // "file-2"
72 echo increment_string('file_4'); // "file_5"
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020075.. php:function:: alternator($args)
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreev442682e2012-11-08 22:52:12 +020077 :param mixed $args: A variable number of arguments
Andrey Andreev3de130c2014-02-07 23:31:49 +020078 :returns: Alternated string(s)
79 :rtype: mixed
Andrey Andreev442682e2012-11-08 22:52:12 +020080
Derek Jones4b184f32013-07-19 16:41:57 -070081 Allows two or more items to be alternated between, when cycling through
82 a loop. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Derek Jones4b184f32013-07-19 16:41:57 -070084 for ($i = 0; $i < 10; $i++)
85 {     
86 echo alternator('string one', 'string two');
87 }
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jones4b184f32013-07-19 16:41:57 -070089 You can add as many parameters as you want, and with each iteration of
90 your loop the next item will be returned.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Derek Jones4b184f32013-07-19 16:41:57 -070092 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050093
Derek Jones4b184f32013-07-19 16:41:57 -070094 for ($i = 0; $i < 10; $i++)
95 {     
96 echo alternator('one', 'two', 'three', 'four', 'five');
97 }
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Derek Jones4b184f32013-07-19 16:41:57 -070099 .. note:: To use multiple separate calls to this function simply call the
100 function with no arguments to re-initialize.
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200102.. php:function:: repeater($data[, $num = 1])
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200104 :param string $data: Input
105 :param int $num: Number of times to repeat
Andrey Andreev3de130c2014-02-07 23:31:49 +0200106 :returns: Repeated string
107 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jones4b184f32013-07-19 16:41:57 -0700109 Generates repeating copies of the data you submit. Example::
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200110
Derek Jones4b184f32013-07-19 16:41:57 -0700111 $string = "\n";
112 echo repeater($string, 30);
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Derek Jones4b184f32013-07-19 16:41:57 -0700114 The above would generate 30 newlines.
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Derek Jones4b184f32013-07-19 16:41:57 -0700116 .. note:: This function is DEPRECATED. Use the native ``str_repeat()``
117 instead.
Andrey Andreev60826db2012-10-27 14:45:23 +0300118
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200120.. php:function:: reduce_double_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev442682e2012-11-08 22:52:12 +0200122 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200123 :returns: A string with normalized slashes
124 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200125
Derek Jones4b184f32013-07-19 16:41:57 -0700126 Converts double slashes in a string to a single slash, except those
Master Yodabd2a7e42015-03-25 02:36:31 -0700127 found in URL protocol prefixes (e.g. http&#58;//).
Andrey Andreev442682e2012-11-08 22:52:12 +0200128
Derek Jones4b184f32013-07-19 16:41:57 -0700129 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Derek Jones4b184f32013-07-19 16:41:57 -0700131 $string = "http://example.com//index.php";
132 echo reduce_double_slashes($string); // results in "http://example.com/index.php"
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
tiyowan5511fbf2012-03-15 21:53:07 +0400134
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200135.. php:function:: strip_slashes($data)
tiyowan5511fbf2012-03-15 21:53:07 +0400136
Andrey Andreev3de130c2014-02-07 23:31:49 +0200137 :param mixed $data: Input string or an array of strings
138 :returns: String(s) with stripped slashes
139 :rtype: mixed
tiyowan5511fbf2012-03-15 21:53:07 +0400140
Andrey Andreev3de130c2014-02-07 23:31:49 +0200141 Removes any slashes from an array of strings.
tiyowan5511fbf2012-03-15 21:53:07 +0400142
Derek Jones4b184f32013-07-19 16:41:57 -0700143 Example::
Derek Jonesb8c283a2013-07-19 16:02:53 -0700144
Derek Jones4b184f32013-07-19 16:41:57 -0700145 $str = array(
146 'question'  => 'Is your name O\'reilly?',
147 'answer' => 'No, my name is O\'connor.'
148 );
Derek Jonesb8c283a2013-07-19 16:02:53 -0700149
Derek Jones4b184f32013-07-19 16:41:57 -0700150 $str = strip_slashes($str);
Derek Jonesb8c283a2013-07-19 16:02:53 -0700151
Derek Jones4b184f32013-07-19 16:41:57 -0700152 The above will return the following array::
tiyowan5511fbf2012-03-15 21:53:07 +0400153
Derek Jones4b184f32013-07-19 16:41:57 -0700154 array(
155 'question'  => "Is your name O'reilly?",
156 'answer' => "No, my name is O'connor."
157 );
tiyowan5511fbf2012-03-15 21:53:07 +0400158
Derek Jones4b184f32013-07-19 16:41:57 -0700159 .. note:: For historical reasons, this function will also accept
160 and handle string inputs. This however makes it just an
161 alias for ``stripslashes()``.
Andrey Andreev442682e2012-11-08 22:52:12 +0200162
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200163.. php:function:: trim_slashes($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200165 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200166 :returns: Slash-trimmed string
167 :rtype: string
Andrey Andreevf7c39d62012-11-08 22:01:33 +0200168
Derek Jones4b184f32013-07-19 16:41:57 -0700169 Removes any leading/trailing slashes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Derek Jones4b184f32013-07-19 16:41:57 -0700171 $string = "/this/that/theother/";
172 echo trim_slashes($string); // results in this/that/theother
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Derek Jones4b184f32013-07-19 16:41:57 -0700174 .. note:: This function is DEPRECATED. Use the native ``trim()`` instead:
175 |
176 | trim($str, '/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200178.. php:function:: reduce_multiples($str[, $character = ''[, $trim = FALSE]])
Andrey Andreev442682e2012-11-08 22:52:12 +0200179
180 :param string $str: Text to search in
181 :param string $character: Character to reduce
182 :param bool $trim: Whether to also trim the specified character
Andrey Andreev3de130c2014-02-07 23:31:49 +0200183 :returns: Reduced string
184 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200185
Derek Jones4b184f32013-07-19 16:41:57 -0700186 Reduces multiple instances of a particular character occuring directly
187 after each other. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Derek Jones4b184f32013-07-19 16:41:57 -0700189 $string = "Fred, Bill,, Joe, Jimmy";
190 $string = reduce_multiples($string,","); //results in "Fred, Bill, Joe, Jimmy"
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
Derek Jones4b184f32013-07-19 16:41:57 -0700192 If the third parameter is set to TRUE it will remove occurrences of the
193 character at the beginning and the end of the string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
Derek Jones4b184f32013-07-19 16:41:57 -0700195 $string = ",Fred, Bill,, Joe, Jimmy,";
196 $string = reduce_multiples($string, ", ", TRUE); //results in "Fred, Bill, Joe, Jimmy"
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200198.. php:function:: quotes_to_entities($str)
Derek Jones8ede1a22011-10-05 13:34:52 -0500199
Andrey Andreev442682e2012-11-08 22:52:12 +0200200 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200201 :returns: String with quotes converted to HTML entities
202 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200203
Derek Jones4b184f32013-07-19 16:41:57 -0700204 Converts single and double quotes in a string to the corresponding HTML
205 entities. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Derek Jones4b184f32013-07-19 16:41:57 -0700207 $string = "Joe's \"dinner\"";
208 $string = quotes_to_entities($string); //results in "Joe&#39;s &quot;dinner&quot;"
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
Derek Jones8ede1a22011-10-05 13:34:52 -0500210
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200211.. php:function:: strip_quotes($str)
Andrey Andreev442682e2012-11-08 22:52:12 +0200212
213 :param string $str: Input string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200214 :returns: String with quotes stripped
215 :rtype: string
Andrey Andreev442682e2012-11-08 22:52:12 +0200216
Derek Jones4b184f32013-07-19 16:41:57 -0700217 Removes single and double quotes from a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
Derek Jones4b184f32013-07-19 16:41:57 -0700219 $string = "Joe's \"dinner\"";
220 $string = strip_quotes($string); //results in "Joes dinner"