/
str.php
333 lines (295 loc) · 7.55 KB
/
str.php
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* Part of the Fuel framework.
*
* @package Fuel
* @version 1.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2012 Fuel Development Team
* @link http://fuelphp.com
*/
namespace Fuel\Core;
/**
* String handling with encoding support
*
* PHP needs to be compiled with --enable-mbstring
* or a fallback without encoding support is used
*/
class Str
{
/**
* Truncates a string to the given length. It will optionally preserve
* HTML tags if $is_html is set to true.
*
* @param string $string the string to truncate
* @param int $limit the number of characters to truncate too
* @param string $continuation the string to use to denote it was truncated
* @param bool $is_html whether the string has HTML
* @return string the truncated string
*/
public static function truncate($string, $limit, $continuation = '...', $is_html = false)
{
$offset = 0;
$tags = array();
if ($is_html)
{
// Handle special characters.
preg_match_all('/&[a-z]+;/i', strip_tags($string), $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match)
{
if ($match[0][1] >= $limit)
{
break;
}
$limit += (static::length($match[0][0]) - 1);
}
// Handle all the html tags.
preg_match_all('/<[^>]+>([^<]*)/', $string, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match)
{
if($match[0][1] - $offset >= $limit)
{
break;
}
$tag = static::sub(strtok($match[0][0], " \t\n\r\0\x0B>"), 1);
if($tag[0] != '/')
{
$tags[] = $tag;
}
elseif (end($tags) == static::sub($tag, 1))
{
array_pop($tags);
}
$offset += $match[1][1] - $match[0][1];
}
}
$new_string = static::sub($string, 0, $limit = min(static::length($string), $limit + $offset));
$new_string .= (static::length($string) > $limit ? $continuation : '');
$new_string .= (count($tags = array_reverse($tags)) ? '</'.implode('></',$tags).'>' : '');
return $new_string;
}
/**
* Add's _1 to a string or increment the ending number to allow _2, _3, etc
*
* @param string $str required
* @return string
*/
public static function increment($str, $first = 1, $separator = '_')
{
preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
}
/**
* substr
*
* @param string $str required
* @param int $start required
* @param int|null $length
* @param string $encoding default UTF-8
* @return string
*/
public static function sub($str, $start, $length = null, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
// substr functions don't parse null correctly
$length = is_null($length) ? (function_exists('mb_substr') ? mb_strlen($str, $encoding) : strlen($str)) - $start : $length;
return function_exists('mb_substr')
? mb_substr($str, $start, $length, $encoding)
: substr($str, $start, $length);
}
/**
* strlen
*
* @param string $str required
* @param string $encoding default UTF-8
* @return int
*/
public static function length($str, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
return function_exists('mb_strlen')
? mb_strlen($str, $encoding)
: strlen($str);
}
/**
* lower
*
* @param string $str required
* @param string $encoding default UTF-8
* @return string
*/
public static function lower($str, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
return function_exists('mb_strtolower')
? mb_strtolower($str, $encoding)
: strtolower($str);
}
/**
* upper
*
* @param string $str required
* @param string $encoding default UTF-8
* @return string
*/
public static function upper($str, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
return function_exists('mb_strtoupper')
? mb_strtoupper($str, $encoding)
: strtoupper($str);
}
/**
* lcfirst
*
* Does not strtoupper first
*
* @param string $str required
* @param string $encoding default UTF-8
* @return string
*/
public static function lcfirst($str, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
return function_exists('mb_strtolower')
? mb_strtolower(mb_substr($str, 0, 1, $encoding), $encoding).
mb_substr($str, 1, mb_strlen($str, $encoding), $encoding)
: lcfirst($str);
}
/**
* ucfirst
*
* Does not strtolower first
*
* @param string $str required
* @param string $encoding default UTF-8
* @return string
*/
public static function ucfirst($str, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
return function_exists('mb_strtoupper')
? mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding).
mb_substr($str, 1, mb_strlen($str, $encoding), $encoding)
: ucfirst($str);
}
/**
* ucwords
*
* First strtolower then ucwords
*
* ucwords normally doesn't strtolower first
* but MB_CASE_TITLE does, so ucwords now too
*
* @param string $str required
* @param string $encoding default UTF-8
* @return string
*/
public static function ucwords($str, $encoding = null)
{
$encoding or $encoding = \Fuel::$encoding;
return function_exists('mb_convert_case')
? mb_convert_case($str, MB_CASE_TITLE, $encoding)
: ucwords(strtolower($str));
}
/**
* Creates a random string of characters
*
* @param string the type of string
* @param int the number of characters
* @return string the random string
*/
public static function random($type = 'alnum', $length = 16)
{
switch($type)
{
case 'basic':
return mt_rand();
break;
default:
case 'alnum':
case 'numeric':
case 'nozero':
case 'alpha':
case 'distinct':
case 'hexdec':
switch ($type)
{
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
default:
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
case 'distinct':
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
break;
case 'hexdec':
$pool = '0123456789abcdef';
break;
}
$str = '';
for ($i=0; $i < $length; $i++)
{
$str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
}
return $str;
break;
case 'unique':
return md5(uniqid(mt_rand()));
break;
case 'sha1' :
return sha1(uniqid(mt_rand(), true));
break;
}
}
/**
* Returns a closure that will alternate between the args which to return.
* If you call the closure with false as the arg it will return the value without
* alternating the next time.
*
* @return Closure
*/
public static function alternator()
{
// the args are the values to alternate
$args = func_get_args();
return function ($next = true) use ($args)
{
static $i = 0;
return $args[($next ? $i++ : $i) % count($args)];
};
}
/**
* Parse the params from a string using strtr()
*
* @param string string to parse
* @param array params to str_replace
* @return string
*/
public static function tr($string, $array = array())
{
if (is_string($string))
{
$tr_arr = array();
foreach ($array as $from => $to)
{
$tr_arr[':'.$from] = $to;
}
unset($array);
return strtr($string, $tr_arr);
}
else
{
return $string;
}
}
}