1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
// ------------------------------------------------------------------------ |
12
|
|
|
|
13
|
|
|
if ( ! function_exists('text_split')) { |
14
|
|
|
/** |
15
|
|
|
* text_split |
16
|
|
|
* |
17
|
|
|
* Split text into array without losing any words. |
18
|
|
|
* |
19
|
|
|
* @params string $text |
20
|
|
|
* |
21
|
|
|
* @param string $text Text Source |
22
|
|
|
* @param string $splitter Split Text Marker |
23
|
|
|
* @param int $limit Split after num of limit characters |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
function text_split($text, $splitter = '<---text-split--->', $limit = 100) |
28
|
|
|
{ |
29
|
|
|
$wrap_text = wordwrap($text, $limit, $splitter); |
30
|
|
|
$wrap_text = preg_split('[' . $splitter . ']', $wrap_text, -1, PREG_SPLIT_NO_EMPTY); |
31
|
|
|
|
32
|
|
|
return implode('', $wrap_text); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
// ------------------------------------------------------------------------ |
36
|
|
|
|
37
|
|
|
if ( ! function_exists('text_columns')) { |
38
|
|
|
/** |
39
|
|
|
* text_columns |
40
|
|
|
* |
41
|
|
|
* Split a block of strings or text evenly across a number of columns. |
42
|
|
|
* |
43
|
|
|
* @param string $text Text Source |
44
|
|
|
* @param int $cols Number of columns |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
function text_columns($text, $cols) |
49
|
|
|
{ |
50
|
|
|
$col_length = ceil(strlen($text) / $cols) + 3; |
51
|
|
|
$return = explode("\n", wordwrap(strrev($text), $col_length)); |
|
|
|
|
52
|
|
|
|
53
|
|
|
if (count($return) > $cols) { |
54
|
|
|
$return[ $cols - 1 ] .= " " . $return[ $cols ]; |
55
|
|
|
unset($return[ $cols ]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$return = array_map("strrev", $return); |
59
|
|
|
|
60
|
|
|
return array_reverse($return); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
// ------------------------------------------------------------------------ |
64
|
|
|
|
65
|
|
|
if ( ! function_exists('text_wrap')) { |
66
|
|
|
/** |
67
|
|
|
* text_wrap |
68
|
|
|
* |
69
|
|
|
* Wrap the given string to a certain chars length and lines. |
70
|
|
|
* |
71
|
|
|
* @param string $text Text Source |
72
|
|
|
* @param int $chars 10 means wrap at 40 chars |
73
|
|
|
* @param int|bool $lines True or false $lines = false or $lines = 3, means truncate after 3 lines |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
function text_wrap($text, $chars = 10, $lines = false) |
78
|
|
|
{ |
79
|
|
|
# the simple case - return wrapped words |
80
|
|
|
if ( ! $lines) { |
81
|
|
|
return wordwrap($text, $chars, "\n"); |
82
|
|
|
} |
83
|
|
|
# truncate to maximum possible number of characters |
84
|
|
|
$return = substr($text, 0, $chars * $lines); |
85
|
|
|
# apply wrapping and return first $lines lines |
86
|
|
|
$return = wordwrap($return, $chars, "\n"); |
87
|
|
|
preg_match("/(.+\n?){0,$lines}/", $return, $regs); |
88
|
|
|
|
89
|
|
|
return $regs[ 0 ]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
// ------------------------------------------------------------------------ |
93
|
|
|
|
94
|
|
|
if ( ! function_exists('text_trim')) { |
95
|
|
|
/** |
96
|
|
|
* Cuts the given string to a certain length without breaking a word. |
97
|
|
|
* |
98
|
|
|
* @param string $text Text source |
99
|
|
|
* @param int $limit number of maximum characters leave remaining |
100
|
|
|
* @param string $break = "." break on dot ending or keep maximum set on ' ' |
101
|
|
|
* @param string $ending = '...' to display '...' on the end of the trimmed string |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
function text_trim($text, $limit, $break = '.', $ending = '.') |
106
|
|
|
{ |
107
|
|
|
// return with no change if string is shorter than $limit |
108
|
|
|
if (strlen($text) <= $limit) { |
109
|
|
|
return $text; |
110
|
|
|
} |
111
|
|
|
// is $break present between $limit and the end of the string? |
112
|
|
|
if (false !== ($breakpoint = strpos($text, $break, $limit))) { |
113
|
|
|
if ($breakpoint < strlen($text) - 1) { |
114
|
|
|
$text = substr($text, 0, $breakpoint) . $ending; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $text; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// ------------------------------------------------------------------------ |
123
|
|
|
|
124
|
|
|
if ( ! function_exists('text_word_limiter')) { |
125
|
|
|
/** |
126
|
|
|
* text_word_limiter |
127
|
|
|
* |
128
|
|
|
* Limits a string to X number of words. |
129
|
|
|
* |
130
|
|
|
* @param string |
131
|
|
|
* @param int |
132
|
|
|
* @param string the end character. Usually an ellipsis |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
function text_word_limiter($str, $limit = 100, $end_char = '…') |
137
|
|
|
{ |
138
|
|
|
if (trim($str) === '') { |
139
|
|
|
return $str; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
preg_match('/^\s*+(?:\S++\s*+){1,' . (int)$limit . '}/', $str, $matches); |
143
|
|
|
|
144
|
|
|
if (strlen($str) === strlen($matches[ 0 ])) { |
145
|
|
|
$end_char = ''; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return rtrim($matches[ 0 ]) . $end_char; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// ------------------------------------------------------------------------ |
153
|
|
|
|
154
|
|
|
if ( ! function_exists('text_character_limiter')) { |
155
|
|
|
/** |
156
|
|
|
* text_character_limiter |
157
|
|
|
* |
158
|
|
|
* Limits the string based on the character count. Preserves complete words |
159
|
|
|
* so the character count may not be exactly as specified. |
160
|
|
|
* |
161
|
|
|
* @param string |
162
|
|
|
* @param int |
163
|
|
|
* @param string the end character. Usually an ellipsis |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
function text_character_limiter($string, $n = 500, $end_char = '…') |
168
|
|
|
{ |
169
|
|
|
if (mb_strlen($string) < $n) { |
170
|
|
|
return $string; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// a bit complicated, but faster than preg_replace with \s+ |
174
|
|
|
$string = preg_replace('/ {2,}/', ' ', str_replace(["\r", "\n", "\t", "\x0B", "\x0C"], ' ', $string)); |
175
|
|
|
|
176
|
|
|
if (mb_strlen($string) <= $n) { |
177
|
|
|
return $string; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$out = ''; |
181
|
|
|
foreach (explode(' ', trim($string)) as $val) { |
182
|
|
|
$out .= $val . ' '; |
183
|
|
|
|
184
|
|
|
if (mb_strlen($out) >= $n) { |
185
|
|
|
$out = trim($out); |
186
|
|
|
|
187
|
|
|
return (mb_strlen($out) === mb_strlen($string)) ? $out : $out . $end_char; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// ------------------------------------------------------------------------ |
194
|
|
|
|
195
|
|
|
if ( ! function_exists('text_censored')) { |
196
|
|
|
/** |
197
|
|
|
* text_censored |
198
|
|
|
* |
199
|
|
|
* Supply a string and an array of disallowed words and any |
200
|
|
|
* matched words will be converted to #### or to the replacement |
201
|
|
|
* word you've submitted. |
202
|
|
|
* |
203
|
|
|
* @param string the text string |
204
|
|
|
* @param string the array of censoered words |
205
|
|
|
* @param string the optional replacement value |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
function text_censored($string, $censored, $replacement = '') |
210
|
|
|
{ |
211
|
|
|
if ( ! is_array($censored)) { |
212
|
|
|
return $string; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$string = ' ' . $string . ' '; |
216
|
|
|
|
217
|
|
|
// \w, \b and a few others do not match on a unicode character |
218
|
|
|
// set for performance reasons. As a result words like �ber |
219
|
|
|
// will not match on a word boundary. Instead, we'll assume that |
220
|
|
|
// a bad word will be bookeneded by any of these characters. |
221
|
|
|
$delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; |
222
|
|
|
|
223
|
|
|
foreach ($censored as $badword) { |
224
|
|
|
if ($replacement !== '') { |
225
|
|
|
$string = preg_replace( |
226
|
|
|
"/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", |
227
|
|
|
"\\1{$replacement}\\3", |
228
|
|
|
$string |
229
|
|
|
); |
230
|
|
|
} else { |
231
|
|
|
$string = preg_replace( |
232
|
|
|
"/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", |
233
|
|
|
"'\\1'.str_repeat('#', strlen('\\2')).'\\3'", |
234
|
|
|
$string |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return trim($string); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// ------------------------------------------------------------------------ |
244
|
|
|
|
245
|
|
|
if ( ! function_exists('text_highlight_phrase')) { |
246
|
|
|
/** |
247
|
|
|
* text_highlight_phrase |
248
|
|
|
* |
249
|
|
|
* Highlights a phrase within a text string. |
250
|
|
|
* |
251
|
|
|
* @param string $string the text string |
252
|
|
|
* @param string $phrase the phrase you'd like to highlight |
253
|
|
|
* @param string $tag_open the openging tag to precede the phrase with |
254
|
|
|
* @param string $tag_close the closing tag to end the phrase with |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
function text_highlight_phrase($string, $phrase, $tag_open = '<mark>', $tag_close = '</mark>') |
259
|
|
|
{ |
260
|
|
|
return ($string !== '' && $phrase !== '') |
261
|
|
|
? preg_replace( |
262
|
|
|
'/(' . preg_quote($phrase, '/') . ')/i' . ('UTF8_ENABLED' ? 'u' : ''), |
263
|
|
|
$tag_open . '\\1' . $tag_close, |
264
|
|
|
$string |
265
|
|
|
) |
266
|
|
|
: $string; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
// ------------------------------------------------------------------------ |
271
|
|
|
|
272
|
|
|
if ( ! function_exists('text_word_wrap')) { |
273
|
|
|
/** |
274
|
|
|
* text_word_wrap |
275
|
|
|
* |
276
|
|
|
* Wraps text at the specified character. Maintains the integrity of words. |
277
|
|
|
* Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor |
278
|
|
|
* will URLs. |
279
|
|
|
* |
280
|
|
|
* @param string $string the text string |
281
|
|
|
* @param int $limit = 76 the number of characters to wrap at |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
function text_word_wrap($string, $limit = 76) |
286
|
|
|
{ |
287
|
|
|
// Set the character limit |
288
|
|
|
is_numeric($limit) OR $limit = 76; |
289
|
|
|
|
290
|
|
|
// Reduce multiple spaces |
291
|
|
|
$string = preg_replace('| +|', ' ', $string); |
292
|
|
|
|
293
|
|
|
// Standardize newlines |
294
|
|
|
if (strpos($string, "\r") !== false) { |
295
|
|
|
$string = str_replace(["\r\n", "\r"], "\n", $string); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// If the current word is surrounded by {unwrap} tags we'll |
299
|
|
|
// strip the entire chunk and replace it with a marker. |
300
|
|
|
$unwrap = []; |
301
|
|
|
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $string, $matches)) { |
302
|
|
|
for ($i = 0, $c = count($matches[ 0 ]); $i < $c; $i++) { |
303
|
|
|
$unwrap[] = $matches[ 1 ][ $i ]; |
304
|
|
|
$string = str_replace($matches[ 0 ][ $i ], '{{unwrapped' . $i . '}}', $string); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// Use PHP's native function to do the initial wordwrap. |
309
|
|
|
// We set the cut flag to FALSE so that any individual words that are |
310
|
|
|
// too long get left alone. In the next step we'll deal with them. |
311
|
|
|
$string = wordwrap($string, $limit, "\n", false); |
312
|
|
|
|
313
|
|
|
// Split the string into individual lines of text and cycle through them |
314
|
|
|
$output = ''; |
315
|
|
|
foreach (explode("\n", $string) as $line) { |
316
|
|
|
// Is the line within the allowed character count? |
317
|
|
|
// If so we'll join it to the output and continue |
318
|
|
|
if (mb_strlen($line) <= $limit) { |
319
|
|
|
$output .= $line . "\n"; |
320
|
|
|
continue; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$temp = ''; |
324
|
|
|
while (mb_strlen($line) > $limit) { |
325
|
|
|
// If the over-length word is a URL we won't wrap it |
326
|
|
|
if (preg_match('!\[url.+\]|://|www\.!', $line)) { |
327
|
|
|
break; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// Trim the word down |
331
|
|
|
$temp .= mb_substr($line, 0, $limit - 1); |
332
|
|
|
$line = mb_substr($line, $limit - 1); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// If $temp contains data it means we had to split up an over-length |
336
|
|
|
// word into smaller chunks so we'll add it back to our current line |
337
|
|
|
if ($temp !== '') { |
338
|
|
|
$output .= $temp . "\n" . $line . "\n"; |
339
|
|
|
} else { |
340
|
|
|
$output .= $line . "\n"; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
// Put our markers back |
345
|
|
|
if (count($unwrap) > 0) { |
346
|
|
|
foreach ($unwrap as $key => $val) { |
347
|
|
|
$output = str_replace('{{unwrapped' . $key . '}}', $val, $output); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return $output; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
// ------------------------------------------------------------------------ |
356
|
|
|
|
357
|
|
|
if ( ! function_exists('text_ellipsis')) { |
358
|
|
|
/** |
359
|
|
|
* text_ellipsis |
360
|
|
|
* |
361
|
|
|
* This function will strip tags from a string, split it at its max_length and ellipsis |
362
|
|
|
* |
363
|
|
|
* @param string string to ellipsize |
364
|
|
|
* @param int max length of string |
|
|
|
|
365
|
|
|
* @param mixed int (1|0) or float, .5, .2, etc for position to split |
366
|
|
|
* @param string ellipsis ; Default '...' |
|
|
|
|
367
|
|
|
* |
368
|
|
|
* @return string ellipsis string |
369
|
|
|
*/ |
370
|
|
|
function text_ellipsis($string, $max_length, $position = 1, $ellipsis = '…') |
371
|
|
|
{ |
372
|
|
|
// Strip tags |
373
|
|
|
$string = trim(strip_tags($string)); |
374
|
|
|
|
375
|
|
|
// Is the string long enough to ellipsis? |
376
|
|
|
if (mb_strlen($string) <= $max_length) { |
377
|
|
|
return $string; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$beg = mb_substr($string, 0, floor($max_length * $position)); |
|
|
|
|
381
|
|
|
$position = ($position > 1) ? 1 : $position; |
382
|
|
|
|
383
|
|
|
if ($position === 1) { |
384
|
|
|
$end = mb_substr($string, 0, -($max_length - mb_strlen($beg))); |
385
|
|
|
} else { |
386
|
|
|
$end = mb_substr($string, -($max_length - mb_strlen($beg))); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
return $beg . $ellipsis . $end; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// ------------------------------------------------------------------------ |
394
|
|
|
|
395
|
|
|
if ( ! function_exists('text_excerpt')) { |
396
|
|
|
/** |
397
|
|
|
* text_excerpt |
398
|
|
|
* |
399
|
|
|
* Allows to extract a piece of text surrounding a word or phrase. |
400
|
|
|
* |
401
|
|
|
* @param string $string String to search the phrase |
402
|
|
|
* @param string $phrase Phrase that will be searched for. |
403
|
|
|
* @param int $radius The amount of characters returned arround the phrase. |
404
|
|
|
* @param string $ellipsis Ending that will be appended |
405
|
|
|
* |
406
|
|
|
* @return string |
407
|
|
|
* |
408
|
|
|
* If no $phrase is passed, will generate an excerpt of $radius characters |
409
|
|
|
* from the begining of $text. |
410
|
|
|
*/ |
411
|
|
|
function text_excerpt($string, $phrase = null, $radius = 100, $ellipsis = '...') |
412
|
|
|
{ |
413
|
|
|
if (isset($phrase)) { |
414
|
|
|
$phrase_pos = strpos(strtolower($string), strtolower($phrase)); |
415
|
|
|
$phrase_len = strlen($phrase); |
416
|
|
|
} elseif ( ! isset($phrase)) { |
417
|
|
|
$phrase_pos = $radius / 2; |
418
|
|
|
$phrase_len = 1; |
419
|
|
|
} |
420
|
|
|
$pre = explode(' ', substr($string, 0, $phrase_pos)); |
|
|
|
|
421
|
|
|
$pos = explode(' ', substr($string, $phrase_pos + $phrase_len)); |
|
|
|
|
422
|
|
|
$prev = ' '; |
423
|
|
|
$post = ' '; |
424
|
|
|
$count = 0; |
425
|
|
|
foreach (array_reverse($pre) as $pr => $e) { |
426
|
|
|
if ((strlen($e) + $count + 1) < $radius) { |
427
|
|
|
$prev = ' ' . $e . $prev; |
428
|
|
|
} |
429
|
|
|
$count = ++$count + strlen($e); |
430
|
|
|
} |
431
|
|
|
$count = 0; |
432
|
|
|
foreach ($pos as $po => $s) { |
433
|
|
|
if ((strlen($s) + $count + 1) < $radius) { |
434
|
|
|
$post .= $s . ' '; |
435
|
|
|
} |
436
|
|
|
$count = ++$count + strlen($s); |
437
|
|
|
} |
438
|
|
|
$ellPre = $phrase ? $ellipsis : ''; |
439
|
|
|
|
440
|
|
|
return $ellPre . $prev . $phrase . $post . $ellipsis; |
441
|
|
|
} |
442
|
|
|
} |