3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
use Config\ForeignCharacters;
16
// CodeIgniter Text Helpers
18
if (! function_exists('word_limiter')) {
22
* Limits a string to X number of words.
24
* @param string $endChar the end character. Usually an ellipsis
26
function word_limiter(string $str, int $limit = 100, string $endChar = '…'): string
28
if (trim($str) === '') {
32
preg_match('/^\s*+(?:\S++\s*+){1,' . $limit . '}/', $str, $matches);
34
if (strlen($str) === strlen($matches[0])) {
38
return rtrim($matches[0]) . $endChar;
42
if (! function_exists('character_limiter')) {
46
* Limits the string based on the character count. Preserves complete words
47
* so the character count may not be exactly as specified.
49
* @param string $endChar the end character. Usually an ellipsis
51
function character_limiter(string $str, int $n = 500, string $endChar = '…'): string
53
if (mb_strlen($str) < $n) {
57
// a bit complicated, but faster than preg_replace with \s+
58
$str = preg_replace('/ {2,}/', ' ', str_replace(["\r", "\n", "\t", "\x0B", "\x0C"], ' ', $str));
60
if (mb_strlen($str) <= $n) {
66
foreach (explode(' ', trim($str)) as $val) {
68
if (mb_strlen($out) >= $n) {
74
return (mb_strlen($out) === mb_strlen($str)) ? $out : $out . $endChar;
78
if (! function_exists('ascii_to_entities')) {
80
* High ASCII to Entities
82
* Converts high ASCII text and MS Word special characters to character entities
84
function ascii_to_entities(string $str): string
88
for ($i = 0, $s = strlen($str) - 1, $count = 1, $temp = []; $i <= $s; $i++) {
89
$ordinal = ord($str[$i]);
93
If the $temp array has a value but we have moved on, then it seems only
94
fair that we output that entity and restart $temp before continuing.
96
if (count($temp) === 1) {
97
$out .= '&#' . array_shift($temp) . ';';
104
$count = ($ordinal < 224) ? 2 : 3;
109
if (count($temp) === $count) {
110
$number = ($count === 3) ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) : (($temp[0] % 32) * 64) + ($temp[1] % 64);
111
$out .= '&#' . $number . ';';
115
// If this is the last iteration, just output whatever we have
117
$out .= '&#' . implode(';', $temp) . ';';
126
if (! function_exists('entities_to_ascii')) {
130
* Converts character entities back to ASCII
132
function entities_to_ascii(string $str, bool $all = true): string
134
if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
135
for ($i = 0, $s = count($matches[0]); $i < $s; $i++) {
136
$digits = (int) $matches[1][$i];
139
$out .= chr($digits);
140
} elseif ($digits < 2048) {
141
$out .= chr(192 + (($digits - ($digits % 64)) / 64)) . chr(128 + ($digits % 64));
143
$out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
144
. chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
145
. chr(128 + ($digits % 64));
147
$str = str_replace($matches[0][$i], $out, $str);
153
['&', '<', '>', '"', ''', '-'],
154
['&', '<', '>', '"', "'", '-'],
163
if (! function_exists('word_censor')) {
165
* Word Censoring Function
167
* Supply a string and an array of disallowed words and any
168
* matched words will be converted to #### or to the replacement
169
* word you've submitted.
171
* @param string $str the text string
172
* @param array $censored the array of censored words
173
* @param string $replacement the optional replacement value
175
function word_censor(string $str, array $censored, string $replacement = ''): string
177
if ($censored === []) {
181
$str = ' ' . $str . ' ';
183
// \w, \b and a few others do not match on a unicode character
184
// set for performance reasons. As a result words like über
185
// will not match on a word boundary. Instead, we'll assume that
186
// a bad word will be bookended by any of these characters.
187
$delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
189
foreach ($censored as $badword) {
190
$badword = str_replace('\*', '\w*?', preg_quote($badword, '/'));
192
if ($replacement !== '') {
194
"/({$delim})(" . $badword . ")({$delim})/i",
195
"\\1{$replacement}\\3",
198
} elseif (preg_match_all("/{$delim}(" . $badword . "){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE)) {
199
$matches = $matches[1];
201
for ($i = count($matches) - 1; $i >= 0; $i--) {
202
$length = strlen($matches[$i][0]);
204
$str = substr_replace(
206
str_repeat('#', $length),
218
if (! function_exists('highlight_code')) {
222
* Colorizes code strings
224
* @param string $str the text string
226
function highlight_code(string $str): string
228
/* The highlight string function encodes and highlights
229
* brackets so we need them to start raw.
231
* Also replace any existing PHP tags to temporary markers
232
* so they don't accidentally break the string out of PHP,
233
* and thus, thwart the highlighting.
236
['<', '>', '<?', '?>', '<%', '%>', '\\', '</script>'],
237
['<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'],
241
// The highlight_string function requires that the text be surrounded
242
// by PHP tags, which we will remove later
243
$str = highlight_string('<?php ' . $str . ' ?>', true);
245
// Remove our artificially added PHP, and the syntax highlighting that came with it
248
'/<span style="color: #([A-Z0-9]+)"><\?php( | )/i',
249
'/(<span style="color: #[A-Z0-9]+">.*?)\?><\/span>\n<\/span>\n<\/code>/is',
250
'/<span style="color: #[A-Z0-9]+"\><\/span>/i',
253
'<span style="color: #$1">',
254
"$1</span>\n</span>\n</code>",
260
// Replace our markers back to PHP tags.
283
if (! function_exists('highlight_phrase')) {
287
* Highlights a phrase within a text string
289
* @param string $str the text string
290
* @param string $phrase the phrase you'd like to highlight
291
* @param string $tagOpen the opening tag to precede the phrase with
292
* @param string $tagClose the closing tag to end the phrase with
294
function highlight_phrase(string $str, string $phrase, string $tagOpen = '<mark>', string $tagClose = '</mark>'): string
296
return ($str !== '' && $phrase !== '') ? preg_replace('/(' . preg_quote($phrase, '/') . ')/i', $tagOpen . '\\1' . $tagClose, $str) : $str;
300
if (! function_exists('convert_accented_characters')) {
302
* Convert Accented Foreign Characters to ASCII
304
* @param string $str Input string
306
function convert_accented_characters(string $str): string
308
static $arrayFrom, $arrayTo;
310
if (! is_array($arrayFrom)) {
311
$config = new ForeignCharacters();
313
if ($config->characterList === [] || ! is_array($config->characterList)) {
319
$arrayFrom = array_keys($config->characterList);
320
$arrayTo = array_values($config->characterList);
325
return preg_replace($arrayFrom, $arrayTo, $str);
329
if (! function_exists('word_wrap')) {
333
* Wraps text at the specified character. Maintains the integrity of words.
334
* Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
337
* @param string $str the text string
338
* @param int $charlim = 76 the number of characters to wrap at
340
function word_wrap(string $str, int $charlim = 76): string
342
// Reduce multiple spaces
343
$str = preg_replace('| +|', ' ', $str);
345
// Standardize newlines
346
if (str_contains($str, "\r")) {
347
$str = str_replace(["\r\n", "\r"], "\n", $str);
350
// If the current word is surrounded by {unwrap} tags we'll
351
// strip the entire chunk and replace it with a marker.
354
if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches)) {
355
for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
356
$unwrap[] = $matches[1][$i];
357
$str = str_replace($matches[0][$i], '{{unwrapped' . $i . '}}', $str);
361
// Use PHP's native function to do the initial wordwrap.
362
// We set the cut flag to FALSE so that any individual words that are
363
// too long get left alone. In the next step we'll deal with them.
364
$str = wordwrap($str, $charlim, "\n", false);
366
// Split the string into individual lines of text and cycle through them
369
foreach (explode("\n", $str) as $line) {
370
// Is the line within the allowed character count?
371
// If so we'll join it to the output and continue
372
if (mb_strlen($line) <= $charlim) {
373
$output .= $line . "\n";
380
while (mb_strlen($line) > $charlim) {
381
// If the over-length word is a URL we won't wrap it
382
if (preg_match('!\[url.+\]|://|www\.!', $line)) {
385
// Trim the word down
386
$temp .= mb_substr($line, 0, $charlim - 1);
387
$line = mb_substr($line, $charlim - 1);
390
// If $temp contains data it means we had to split up an over-length
391
// word into smaller chunks so we'll add it back to our current line
393
$output .= $temp . "\n" . $line . "\n";
395
$output .= $line . "\n";
399
// Put our markers back
400
foreach ($unwrap as $key => $val) {
401
$output = str_replace('{{unwrapped' . $key . '}}', $val, $output);
404
// remove any trailing newline
405
return rtrim($output);
409
if (! function_exists('ellipsize')) {
413
* This function will strip tags from a string, split it at its max_length and ellipsize
415
* @param string $str String to ellipsize
416
* @param int $maxLength Max length of string
417
* @param float|int $position int (1|0) or float, .5, .2, etc for position to split
418
* @param string $ellipsis ellipsis ; Default '...'
420
* @return string Ellipsized string
422
function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis = '…'): string
425
$str = trim(strip_tags($str));
427
// Is the string long enough to ellipsize?
428
if (mb_strlen($str) <= $maxLength) {
432
$beg = mb_substr($str, 0, (int) floor($maxLength * $position));
433
$position = ($position > 1) ? 1 : $position;
435
if ($position === 1) {
436
$end = mb_substr($str, 0, -($maxLength - mb_strlen($beg)));
438
$end = mb_substr($str, -($maxLength - mb_strlen($beg)));
441
return $beg . $ellipsis . $end;
445
if (! function_exists('strip_slashes')) {
449
* Removes slashes contained in a string or in an array
451
* @param array|string $str string or array
453
* @return array|string string or array
455
function strip_slashes($str)
457
if (! is_array($str)) {
458
return stripslashes($str);
461
foreach ($str as $key => $val) {
462
$str[$key] = strip_slashes($val);
469
if (! function_exists('strip_quotes')) {
473
* Removes single and double quotes from a string
475
function strip_quotes(string $str): string
477
return str_replace(['"', "'"], '', $str);
481
if (! function_exists('quotes_to_entities')) {
485
* Converts single and double quotes to entities
487
function quotes_to_entities(string $str): string
489
return str_replace(["\\'", '"', "'", '"'], [''', '"', ''', '"'], $str);
493
if (! function_exists('reduce_double_slashes')) {
495
* Reduce Double Slashes
497
* Converts double slashes in a string to a single slash,
498
* except those found in http://
500
* http://www.some-site.com//index.php
504
* http://www.some-site.com/index.php
506
function reduce_double_slashes(string $str): string
508
return preg_replace('#(^|[^:])//+#', '\\1/', $str);
512
if (! function_exists('reduce_multiples')) {
516
* Reduces multiple instances of a particular character. Example:
518
* Fred, Bill,, Joe, Jimmy
522
* Fred, Bill, Joe, Jimmy
524
* @param string $character the character you wish to reduce
525
* @param bool $trim TRUE/FALSE - whether to trim the character from the beginning/end
527
function reduce_multiples(string $str, string $character = ',', bool $trim = false): string
529
$pattern = '#' . preg_quote($character, '#') . '{2,}#';
530
$str = preg_replace($pattern, $character, $str);
532
return $trim ? trim($str, $character) : $str;
536
if (! function_exists('random_string')) {
538
* Create a Random String
540
* Useful for generating passwords or hashes.
542
* @param string $type Type of random string. basic, alpha, alnum, numeric, nozero, md5, sha1, and crypto
543
* @param int $len Number of characters
545
* @deprecated The type 'basic', 'md5', and 'sha1' are deprecated. They are not cryptographically secure.
547
function random_string(string $type = 'alnum', int $len = 8): string
555
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
559
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
567
return _from_random($len, $pool);
570
$max = 10 ** $len - 1;
571
$rand = random_int(0, $max);
573
return sprintf('%0' . $len . 'd', $rand);
576
return md5(uniqid((string) mt_rand(), true));
579
return sha1(uniqid((string) mt_rand(), true));
582
if ($len % 2 !== 0) {
583
throw new InvalidArgumentException(
584
'You must set an even number to the second parameter when you use `crypto`.'
588
return bin2hex(random_bytes($len / 2));
591
// 'basic' type treated as default
592
return (string) mt_rand();
596
if (! function_exists('_from_random')) {
598
* The following function was derived from code of Symfony (v6.2.7 - 2023-02-28)
599
* https://github.com/symfony/symfony/blob/80cac46a31d4561804c17d101591a4f59e6db3a2/src/Symfony/Component/String/ByteString.php#L45
600
* Code subject to the MIT license (https://github.com/symfony/symfony/blob/v6.2.7/LICENSE).
601
* Copyright (c) 2004-present Fabien Potencier
603
* The following method was derived from code of the Hack Standard Library (v4.40 - 2020-05-03)
604
* https://github.com/hhvm/hsl/blob/80a42c02f036f72a42f0415e80d6b847f4bf62d5/src/random/private.php#L16
605
* Code subject to the MIT license (https://github.com/hhvm/hsl/blob/master/LICENSE).
606
* Copyright (c) 2004-2020, Facebook, Inc. (https://www.facebook.com/)
608
* @internal Outside the framework this should not be used directly.
610
function _from_random(int $length, string $pool): string
613
throw new InvalidArgumentException(
614
sprintf('A strictly positive length is expected, "%d" given.', $length)
618
$poolSize = \strlen($pool);
619
$bits = (int) ceil(log($poolSize, 2.0));
620
if ($bits <= 0 || $bits > 56) {
621
throw new InvalidArgumentException(
622
'The length of the alphabet must in the [2^1, 2^56] range.'
628
while ($length > 0) {
629
$urandomLength = (int) ceil(2 * $length * $bits / 8.0);
630
$data = random_bytes($urandomLength);
634
for ($i = 0; $i < $urandomLength && $length > 0; $i++) {
636
$unpackedData = ($unpackedData << 8) | \ord($data[$i]);
639
// While we have enough bits to select a character from the alphabet, keep
640
// consuming the random data
641
for (; $unpackedBits >= $bits && $length > 0; $unpackedBits -= $bits) {
642
$index = ($unpackedData & ((1 << $bits) - 1));
643
$unpackedData >>= $bits;
644
// Unfortunately, the alphabet size is not necessarily a power of two.
645
// Worst case, it is 2^k + 1, which means we need (k+1) bits and we
646
// have around a 50% chance of missing as k gets larger
647
if ($index < $poolSize) {
648
$string .= $pool[$index];
659
if (! function_exists('increment_string')) {
661
* Add's _1 to a string or increment the ending number to allow _2, _3, etc
663
* @param string $str Required
664
* @param string $separator What should the duplicate number be appended with
665
* @param int $first Which number should be used for the first dupe increment
667
function increment_string(string $str, string $separator = '_', int $first = 1): string
669
preg_match('/(.+)' . preg_quote($separator, '/') . '([0-9]+)$/', $str, $match);
671
return isset($match[2]) ? $match[1] . $separator . ((int) $match[2] + 1) : $str . $separator . $first;
675
if (! function_exists('alternator')) {
679
* Allows strings to be alternated. See docs...
681
* @param string ...$args (as many parameters as needed)
683
function alternator(...$args): string
687
if (func_num_args() === 0) {
693
return $args[($i++ % count($args))];
697
if (! function_exists('excerpt')) {
701
* Allows to extract a piece of text surrounding a word or phrase.
703
* @param string $text String to search the phrase
704
* @param string $phrase Phrase that will be searched for.
705
* @param int $radius The amount of characters returned around the phrase.
706
* @param string $ellipsis Ending that will be appended
708
* If no $phrase is passed, will generate an excerpt of $radius characters
709
* from the beginning of $text.
711
function excerpt(string $text, ?string $phrase = null, int $radius = 100, string $ellipsis = '...'): string
713
if (isset($phrase)) {
714
$phrasePos = stripos($text, $phrase);
715
$phraseLen = strlen($phrase);
717
$phrasePos = $radius / 2;
721
$pre = explode(' ', substr($text, 0, $phrasePos));
722
$pos = explode(' ', substr($text, $phrasePos + $phraseLen));
728
foreach (array_reverse($pre) as $e) {
729
if ((strlen($e) + $count + 1) < $radius) {
730
$prev = ' ' . $e . $prev;
732
$count = ++$count + strlen($e);
737
foreach ($pos as $s) {
738
if ((strlen($s) + $count + 1) < $radius) {
741
$count = ++$count + strlen($s);
744
$ellPre = $phrase ? $ellipsis : '';
746
return str_replace(' ', ' ', $ellPre . $prev . $phrase . $post . $ellipsis);