blob: 42bcd57adeade0804ba86fa689e65eea5ab24922 [file] [log] [blame]
Derek Allard3d879d52008-01-18 19:41:32 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard3d879d52008-01-18 19:41:32 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter String Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/string_helper.html
Derek Allard3d879d52008-01-18 19:41:32 +000026 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Trim Slashes
32 *
33 * Removes any leading/traling slashes from a string:
34 *
35 * /this/that/theother/
36 *
37 * becomes:
38 *
39 * this/that/theother
40 *
41 * @access public
42 * @param string
43 * @return string
44 */
45function trim_slashes($str)
46{
47 return trim($str, '/');
48}
49
50// ------------------------------------------------------------------------
51
52/**
53 * Strip Slashes
54 *
55 * Removes slashes contained in a string or in an array
56 *
57 * @access public
58 * @param mixed string or array
Derek Allardc04a9c42008-01-18 20:47:31 +000059 * @return mixed string or array
Derek Allard3d879d52008-01-18 19:41:32 +000060 */
61 function strip_slashes($str)
62 {
63 if (is_array($str))
64 {
65 foreach ($str as $key => $val)
66 {
67 $str[$key] = strip_slashes($val);
68 }
69 }
70 else
71 {
72 $str = stripslashes($str);
73 }
74
75 return $str;
76}
77
78// ------------------------------------------------------------------------
79
80/**
81 * Strip Quotes
82 *
83 * Removes single and double quotes from a string
84 *
85 * @access public
86 * @param string
87 * @return string
88 */
89function strip_quotes($str)
90{
91 return str_replace(array('"', "'"), '', $str);
92}
93
94// ------------------------------------------------------------------------
95
96/**
97 * Quotes to Entities
98 *
99 * Converts single and double quotes to entities
100 *
101 * @access public
102 * @param string
103 * @return string
104 */
105function quotes_to_entities($str)
106{
107 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
108}
109
110// ------------------------------------------------------------------------
111/**
112 * Reduce Double Slashes
113 *
114 * Converts double slashes in a string to a single slash,
115 * except those found in http://
116 *
117 * http://www.some-site.com//index.php
118 *
119 * becomes:
120 *
121 * http://www.some-site.com/index.php
122 *
123 * @access public
124 * @param string
125 * @return string
126 */
127function reduce_double_slashes($str)
128{
129 return preg_replace("#([^:])//+#", "\\1/", $str);
130}
131
132// ------------------------------------------------------------------------
133
134/**
135 * Reduce Multiples
136 *
137 * Reduces multiple instances of a particular character. Example:
138 *
139 * Fred, Bill,, Joe, Jimmy
140 *
141 * becomes:
142 *
143 * Fred, Bill, Joe, Jimmy
144 *
145 * @access public
146 * @param string
147 * @param string the character you wish to reduce
148 * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
149 * @return string
150 */
151function reduce_multiples($str, $character = ',', $trim = FALSE)
152{
153 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
154
155 if ($trim === TRUE)
156 {
157 $str = trim($str, $character);
158 }
159
160 return $str;
161}
162
163// ------------------------------------------------------------------------
164
165/**
166 * Create a Random String
167 *
168 * Useful for generating passwords or hashes.
169 *
170 * @access public
171 * @param string type of random string. Options: alunum, numeric, nozero, unique
172 * @param integer number of characters
173 * @return string
174 */
175function random_string($type = 'alnum', $len = 8)
176{
177 switch($type)
178 {
179 case 'alnum' :
180 case 'numeric' :
181 case 'nozero' :
182
183 switch ($type)
184 {
185 case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
186 break;
187 case 'numeric' : $pool = '0123456789';
188 break;
189 case 'nozero' : $pool = '123456789';
190 break;
191 }
192
193 $str = '';
194 for ($i=0; $i < $len; $i++)
195 {
196 $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
197 }
198 return $str;
199 break;
200 case 'unique' : return md5(uniqid(mt_rand()));
201 break;
202 }
203}
204// ------------------------------------------------------------------------
205
206/**
207 * Alternator
208 *
209 * Allows strings to be alternated. See docs...
210 *
211 * @access public
212 * @param string (as many parameters as needed)
213 * @return string
214 */
215function alternator()
216{
217 static $i;
218
219 if (func_num_args() == 0)
220 {
221 $i = 0;
222 return '';
223 }
224 $args = func_get_args();
225 return $args[($i++ % count($args))];
226}
227
228// ------------------------------------------------------------------------
229
230/**
231 * Repeater function
232 *
233 * @access public
234 * @param string
235 * @param integer number of repeats
236 * @return string
237 */
238function repeater($data, $num = 1)
239{
240 return (($num > 0) ? str_repeat($data, $num) : '');
241}
242
243
244?>