forked from mickaelandrieu/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.php
306 lines (278 loc) · 9.84 KB
/
StringUtils.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
<?php
/*
* Copyright (c) 2011-2014 Lp digital system
*
* This file is part of BackBee.
*
* BackBee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BackBee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BackBee. If not, see <http://www.gnu.org/licenses/>.
*/
namespace BackBee\Utils;
use BackBee\Utils\Exception\InvalidArgumentException;
/**
* @author c.rouillon <charles.rouillon@lp-digital.fr>
* @author Mickaël Andrieu <mickael.andrieu@lp-digital.fr>
*/
class StringUtils
{
/**
* Special chars to be replaced
*
* @var array
*/
protected static $specialCharsArray = [
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
'/[ÍÌÎÏ]/u' => 'I',
'/[íìîï]/u' => 'i',
'/[éèêë]/u' => 'e',
'/[ÉÈÊË]/u' => 'E',
'/[óòôõºö]/u' => 'o',
'/[ÓÒÔÕÖ]/u' => 'O',
'/[úùûü]/u' => 'u',
'/[ÚÙÛÜ]/u' => 'U',
'/ç/' => 'c',
'/Ç/' => 'C',
'/ñ/' => 'n',
'/Ñ/' => 'N',
'/œ/' => 'oe',
'/Œ/' => 'OE',
'/æ/' => 'ae',
'/Æ/' => 'AE',
'/ß/' => 'ss',
];
/**
* Return a mixed array of options according the defaults values provided
*
* @access private
* @param array $options
* @param array $default
* @return array The final options
*/
private static function _getOptions($options, $default)
{
if (is_array($options)) {
foreach ($default as $key => $value) {
if (isset($options[$key])) {
$default[$key] = $options[$key];
}
}
}
return $default;
}
/**
* Convert a string to ASCII
*
* @access public
* @param string $str The string to convert
* @param string $charset The default charset to use
* @return string The converted string
*/
public static function toASCII($str, $charset = 'UTF-8')
{
$asciistr = '';
if (mb_detect_encoding($str, 'UTF-8', true) === false) {
$str = utf8_encode($str);
}
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
iconv_set_encoding('input_encoding', 'UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', $charset);
}
$str = html_entity_decode($str, ENT_QUOTES, $charset);
$strlen = iconv_strlen($str, $charset);
for ($i = 0; $i < $strlen; $i++) {
$char = iconv_substr($str, $i, 1, $charset);
if (!preg_match('/[`\'^~"]+/', $char)) {
if ('UTF-8' === $charset) {
$asciistr .= preg_replace('/[`\'^~"]+/', '', iconv($charset, 'ASCII//TRANSLIT//IGNORE', $char));
} else {
$asciistr .= preg_replace('/[`\'^~"]+/', '', iconv('UTF-8', $charset.'//TRANSLIT//IGNORE', $char));
}
} else {
$asciistr .= $char;
}
}
return $asciistr;
}
/**
* Convert string to utf-8 encoding if need
* @param string $str
* @return string
*/
public static function toUTF8($str)
{
if (!is_null($str) && false === mb_detect_encoding($str, 'UTF-8', true)) {
$str = utf8_encode($str);
}
return $str;
}
/**
* Normalize a string to a valid path file name
*
* @access public
* @param string $str The string to normalize
* @param array $options Convert options to use :
* - extension string The extension to concat to the converted string
* - spacereplace string The character replacement for space characters
* - lengthlimit int The max length of the returned string
* @param string $charset The default charset to use
* @return string The converted string
*/
public static function toPath($str, $options = null, $charset = 'UTF-8')
{
$options = self::_getOptions($options, array(
'extension' => '',
'spacereplace' => null,
'lengthlimit' => 2000,
));
$str = trim(preg_replace('/(?:[^\w\-\.~\+% ]+|%(?![A-Fa-f0-9]{2}))/', '', self::toASCII($str, $charset)));
$str = preg_replace('/\s+/', null === $options['spacereplace'] ? '' : $options['spacereplace'], $str);
return substr($str, 0, $options['lengthlimit']).$options['extension'];
}
/**
* Normalize a string to a valid url path
*
* @access public
* @param string $str The string to normalize
* @param array $options Convert options to use :
* - extension string The extension to concat to the converted string
* - separators string A pattern of space characters to replace
* - spacereplace string The character replacement for space characters
* - lengthlimit int The max length of the returned string
* @param string $charset The default charset to use
* @return string The converted string
*/
public static function urlize($str, $options = null, $charset = 'UTF-8')
{
$options = self::_getOptions($options, array(
'extension' => '',
'separators' => '/[.\'’ ]+/',
'spacereplace' => '-',
'lengthlimit' => 2000,
));
$str = self::replaceSpecialChars($str);
$str = str_replace(array('®', '%', '€', '“', '”', '…', '$', '£', '¥'), array('', 'percent', 'euro', '"', '"', '...', 'dollar', 'livre', 'yen'), $str);
$str = preg_replace($options['separators'], ' ', $str);
$str = preg_replace('/[^A-Za-z0-9_\s\'\:\/[\]-]/','',$str);
return strtolower(preg_replace('/[-]+/', '-', self::toPath($str, $options, $charset)));
}
/**
* Replace special characters in a given string
*
* @access public
* @param string $str The string in which to replace special characters
* @return string The string with the special characters replaced
*/
public static function replaceSpecialChars($str)
{
return preg_replace(array_keys(self::$specialCharsArray), array_values(self::$specialCharsArray), $str);
}
/**
* Return an XML compliant form of string
* @param string $str
* @param Boolean $striptags Are HTML tags to be striped from string
* @return string The XML compliant string
*/
public static function toXmlCompliant($str, $striptags = true)
{
if (true === $striptags) {
$str = strip_tags($str);
}
$str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
$str = str_replace(array('<', '>', '&'), array('<', '>', '&'), $str);
return $str;
}
/**
* Inserts newlines before all HTML line breaks in a string
* @param string $str
* @return string The string
*/
public static function br2nl($str)
{
return preg_replace('/<br[^>]*>\s*/is', "\r\n", $str);
}
/**
* truncate a string
* @param int $start
* @param int length
* @param string ellipsis string
* @return string
* copied from symfony 1.2
*/
public static function truncateText($text, $length = 30, $truncate_string = '...', $truncate_lastspace = false)
{
if ($text == '') {
return '';
}
$mbstring = extension_loaded('mbstring');
if ($mbstring) {
$old_encoding = mb_internal_encoding();
mb_internal_encoding(mb_detect_encoding($text));
}
$strlen = ($mbstring) ? 'mb_strlen' : 'strlen';
$substr = ($mbstring) ? 'mb_substr' : 'substr';
if ($strlen($text) > $length) {
$truncate_text = $substr($text, 0, $length - $strlen($truncate_string));
if ($truncate_lastspace) {
$truncate_text = preg_replace('/\s+?(\S+)?$/', '', $truncate_text);
}
$text = $truncate_text.$truncate_string;
}
if ($mbstring) {
mb_internal_encoding($old_encoding);
}
return $text;
}
/**
*
* @param int $bytes
* @return string
*/
public static function formatBytes($bytes, $roundPrecision = 2)
{
$unit = array('b','kb','mb','gb','tb','pb');
return round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), $roundPrecision).' '.$unit[$i];
}
/**
* Converts a string to boolean
*
* @param string $str
* @return boolean
*/
public static function toBoolean($str)
{
if (!is_string($str)) {
throw new InvalidArgumentException(sprintf('String expected, %s received', gettype($str)));
}
$booleanTrue = array(
'1',
'on',
'true',
'yes',
);
if (in_array($str, $booleanTrue, true)) {
return true;
}
$booleanFalse = array(
'0',
'off',
'false',
'no',
);
if (in_array($str, $booleanFalse, true)) {
return false;
}
return false;
}
}