|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @title String Class |
|
4
|
|
|
* @desc Many useful functions for string manipulation. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Pierre-Henry Soria <[email protected]> |
|
7
|
|
|
* @copyright (c) 2011-2017, Pierre-Henry Soria. All Rights Reserved. |
|
8
|
|
|
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
|
9
|
|
|
* @package PH7 / Framework / Str |
|
10
|
|
|
* @version 1.2 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace PH7\Framework\Str { |
|
14
|
|
|
|
|
15
|
|
|
defined('PH7') or exit('Restricted access'); |
|
16
|
|
|
|
|
17
|
|
|
class Str |
|
18
|
|
|
{ |
|
19
|
|
|
private static $_sRegexDelimiter = '#'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Make a string lowercase. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $sText |
|
25
|
|
|
* |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public function lower($sText) |
|
29
|
|
|
{ |
|
30
|
|
|
return mb_strtolower($sText); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Make a string uppercase. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $sText |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function upper($sText) |
|
41
|
|
|
{ |
|
42
|
|
|
return mb_strtoupper($sText); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Make a string's first character lowercase. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $sText |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function lowerFirst($sText) |
|
53
|
|
|
{ |
|
54
|
|
|
return lcfirst($sText); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Make a string's first character uppercase. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $sText |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function upperFirst($sText) |
|
65
|
|
|
{ |
|
66
|
|
|
return ucfirst($sText); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Uppercase the first character of each word in a string. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $sText |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function upperFirstWords($sText) |
|
77
|
|
|
{ |
|
78
|
|
|
return ucwords($sText); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Count the length of a string and supports the special characters (Asian, Latin, ...). |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $sText |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function length($sText) |
|
89
|
|
|
{ |
|
90
|
|
|
return mb_strlen($sText, PH7_ENCODING); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* String sanitize. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $sText |
|
97
|
|
|
* @param string $sFilter Optionally, The some strings separated by a comma. |
|
98
|
|
|
* @param string $sFlag Optionally, a flag |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
* |
|
102
|
|
|
*/ |
|
103
|
|
|
public function sanitize($sText, $sFilter = null, $sFlag = null) |
|
104
|
|
|
{ |
|
105
|
|
|
$sFlag = (!empty($sFlag)) ? (string) $sFlag : ''; |
|
106
|
|
|
|
|
107
|
|
|
if (!empty($sFilter)) { |
|
108
|
|
|
$aFilters = explode(',', $sFilter); |
|
109
|
|
|
foreach ($aFilters as $sF) |
|
110
|
|
|
$sText = str_replace($sF, $sFlag, $sText); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$sText = preg_replace('/[\r\n\t]+/', '', $sText); // Remove new lines, spaces, tabs |
|
114
|
|
|
$sText = preg_replace('/>[\s]+</', '><', $sText); // Remove new lines, spaces, tabs |
|
115
|
|
|
$sText = preg_replace('/[\s]+/', ' ', $sText); // Remove new lines, spaces, tabs |
|
116
|
|
|
|
|
117
|
|
|
return $sText; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Test the equality of two strings. |
|
122
|
|
|
* |
|
123
|
|
|
* @personal For the PHP AND C functions, strcmp and strcasecmp returns a positive or negative integer value if they are different and 0 if they are equal. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $sText1 |
|
126
|
|
|
* @param string $sText2 |
|
127
|
|
|
* |
|
128
|
|
|
* @return boolean |
|
129
|
|
|
*/ |
|
130
|
|
|
public function equals($sText1, $sText2) |
|
131
|
|
|
{ |
|
132
|
|
|
//return (strcmp($sText1, $sText2) === 0); |
|
|
|
|
|
|
133
|
|
|
return ($sText1 === $sText2); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Equals but not case sensitive. Accepts uppercase and lowercase. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $sText1 |
|
140
|
|
|
* @param string $sText2 |
|
141
|
|
|
* |
|
142
|
|
|
* @return boolean |
|
143
|
|
|
*/ |
|
144
|
|
|
public function equalsIgnoreCase($sText1, $sText2) |
|
145
|
|
|
{ |
|
146
|
|
|
//return (strcasecmp($sText1, $sText2) === 0); |
|
|
|
|
|
|
147
|
|
|
$sText1 = $this->lower($sText1); |
|
148
|
|
|
$sText2 = $this->lower($sText2); |
|
149
|
|
|
return $this->equals($sText1, $sText2); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Find the position of the first occurrence of a specified value in a string. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $sText The string to search in. |
|
156
|
|
|
* @param string $sFindText Value to search. |
|
157
|
|
|
* @param integer $iOffset Default: 0 |
|
158
|
|
|
* |
|
159
|
|
|
* @return integer The position of the first occurrence or -1 if the value to search is not found. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function indexOf($sText, $sFindText, $iOffset = 0) |
|
162
|
|
|
{ |
|
163
|
|
|
$mPos = strpos($sText, $sFindText, $iOffset); |
|
164
|
|
|
|
|
165
|
|
|
if (!is_int($mPos)) |
|
166
|
|
|
return -1; |
|
167
|
|
|
|
|
168
|
|
|
return $mPos; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Find the position of the last occurrence of a specified value in a string. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $sText The string to search in. |
|
175
|
|
|
* @param string $sFindText Value to search. |
|
176
|
|
|
* @param integer $iOffset Default: 0 |
|
177
|
|
|
* |
|
178
|
|
|
* @return integer The position of the last occurrence or -1 if the value to search is not found. |
|
179
|
|
|
*/ |
|
180
|
|
|
public function lastIndexOf($sText, $sFindText, $iOffset = 0) |
|
181
|
|
|
{ |
|
182
|
|
|
$mPos = strrpos($sText, $sFindText, $iOffset); |
|
183
|
|
|
|
|
184
|
|
|
if (!is_int($mPos)) |
|
185
|
|
|
return -1; |
|
186
|
|
|
|
|
187
|
|
|
return $mPos; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Creates a new string by trimming any leading or trailing whitespace from the current string. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $sText |
|
194
|
|
|
* @param string $sCharList Default: " \t\n\r\0\x0B" |
|
195
|
|
|
* |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public function trim($sText, $sCharList = " \t\n\r\0\x0B") |
|
199
|
|
|
{ |
|
200
|
|
|
return trim($sText, $sCharList); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Cut a piece of string to make an extract. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $sText |
|
207
|
|
|
* @param integer $iStart Default: 0 |
|
208
|
|
|
* @param integer $iLength Default: 150 |
|
209
|
|
|
* @param string $sTrimMarker Default: '...' |
|
210
|
|
|
* |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
|
|
public function extract($sText, $iStart = 0, $iLength = 150, $sTrimMarker = '...') |
|
214
|
|
|
{ |
|
215
|
|
|
if (function_exists('mb_strimwidth')) { |
|
216
|
|
|
$sText = mb_strimwidth($sText, $iStart, $iLength, $sTrimMarker, PH7_ENCODING); |
|
217
|
|
|
} else { |
|
218
|
|
|
// Recovers a portion of our content. |
|
219
|
|
|
$sExtract = substr($sText, $iStart, $iLength); |
|
220
|
|
|
|
|
221
|
|
|
// Find the last space after the last word of the extract. |
|
222
|
|
|
if ($iLastSpace = strrpos($sExtract, ' ')) |
|
223
|
|
|
// Cut the chain to the last space. |
|
224
|
|
|
$sText = substr($sText, $iStart, $iLastSpace); |
|
225
|
|
|
else |
|
226
|
|
|
// If the string contains any spaces, we cut the chain with the maximum number of characters given. |
|
227
|
|
|
$sText = substr($sText, $iStart, $iLength); |
|
228
|
|
|
|
|
229
|
|
|
$sText .= $sTrimMarker; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return $sText; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Return the string if the variable is not empty else return empty string. |
|
237
|
|
|
* |
|
238
|
|
|
* @param string $sText |
|
239
|
|
|
* |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
public function get($sText) |
|
243
|
|
|
{ |
|
244
|
|
|
return (!empty($sText)) ? $sText : ''; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Perform a regular expression match. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $sText The string to search in. |
|
251
|
|
|
* @param string $sPattern The RegEx pattern to search for, as a string. |
|
252
|
|
|
* |
|
253
|
|
|
* @return string|null |
|
254
|
|
|
*/ |
|
255
|
|
|
public static function match($sText, $sPattern) |
|
256
|
|
|
{ |
|
257
|
|
|
preg_match_all(self::_regexNormalize($sPattern), $sText, $aMatches, PREG_PATTERN_ORDER); |
|
258
|
|
|
|
|
259
|
|
|
if (!empty($aMatches[1])) |
|
260
|
|
|
$mRet = $aMatches[1]; |
|
261
|
|
|
elseif (!empty($aMatches[0])) |
|
262
|
|
|
$mRet = $aMatches[0]; |
|
263
|
|
|
else |
|
264
|
|
|
$mRet = null; |
|
265
|
|
|
|
|
266
|
|
|
return $mRet; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Check if the string doesn't have any blank spaces. |
|
271
|
|
|
* |
|
272
|
|
|
* @param string $sValue |
|
273
|
|
|
* |
|
274
|
|
|
* @return boolean |
|
275
|
|
|
*/ |
|
276
|
|
|
public static function noSpaces($sValue) |
|
277
|
|
|
{ |
|
278
|
|
|
return strlen(trim($sValue)) > 0; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Escape function, uses the PHP native htmlspecialchars but improved. |
|
283
|
|
|
* |
|
284
|
|
|
* @param array|string $mText |
|
285
|
|
|
* @param boolean $bStrip If TRUE, strip only HTML tags instead of converting them into HTML entities. Less secure. Default: FALSE |
|
286
|
|
|
* |
|
287
|
|
|
* @return array|string The escaped string. |
|
288
|
|
|
*/ |
|
289
|
|
|
public function escape($mText, $bStrip = false) |
|
290
|
|
|
{ |
|
291
|
|
|
return (is_array($mText)) ? $this->arrayEscape($mText, $bStrip) : $this->cEscape($mText, $bStrip); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Escape an array of any dimension. |
|
296
|
|
|
* |
|
297
|
|
|
* @param array $aData |
|
298
|
|
|
* @param boolean $bStrip |
|
299
|
|
|
* |
|
300
|
|
|
* @return array The array escaped. |
|
301
|
|
|
*/ |
|
302
|
|
|
protected function arrayEscape(array $aData, $bStrip) |
|
303
|
|
|
{ |
|
304
|
|
|
foreach ($aData as $sKey => $mValue) |
|
305
|
|
|
$aData[$sKey] = (is_array($mValue)) ? $this->arrayEscape($mValue, $bStrip) : $this->cEscape($mValue, $bStrip); |
|
306
|
|
|
|
|
307
|
|
|
return $aData; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @param string $sText |
|
312
|
|
|
* @param boolean $bStrip |
|
313
|
|
|
* |
|
314
|
|
|
* @return string The text parsed with Str::stripTags() method if $bStrip parameter is TRUE, otherwise with Str::htmlSpecialChars method. |
|
315
|
|
|
*/ |
|
316
|
|
|
protected function cEscape($sText, $bStrip) |
|
317
|
|
|
{ |
|
318
|
|
|
return (true === $bStrip) ? $this->stripTags($sText) : $this->htmlSpecialChars($sText); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @param string $sText |
|
323
|
|
|
* |
|
324
|
|
|
* @return string The text parsed with strip_tag() function |
|
325
|
|
|
*/ |
|
326
|
|
|
protected function stripTags($sText) |
|
327
|
|
|
{ |
|
328
|
|
|
return strip_tags($sText); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* @param string $sText |
|
333
|
|
|
* |
|
334
|
|
|
* @return string The text parsed with htmlspecialchars() function |
|
335
|
|
|
*/ |
|
336
|
|
|
protected function htmlSpecialChars($sText) |
|
337
|
|
|
{ |
|
338
|
|
|
return htmlspecialchars($sText, ENT_QUOTES, 'utf-8'); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
private static function _regexNormalize($sPattern) |
|
342
|
|
|
{ |
|
343
|
|
|
return self::$_sRegexDelimiter . trim($sPattern, self::$_sRegexDelimiter) . self::$_sRegexDelimiter; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
namespace { |
|
350
|
|
|
/** |
|
351
|
|
|
* Alias of the \PH7\Framework\Str\Str::escape() method. |
|
352
|
|
|
*/ |
|
353
|
|
|
function escape($mText, $bStrip = false) |
|
354
|
|
|
{ |
|
355
|
|
|
return (new PH7\Framework\Str\Str)->escape($mText, $bStrip); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.