Skip to content

Commit 6f9cfb2

Browse files
committed
refactor: Rework character_limiter()
1 parent 489fbab commit 6f9cfb2

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

system/Helpers/text_helper.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,40 @@ function word_limiter(string $str, int $limit = 100, string $endChar = '…'
4343
/**
4444
* Character Limiter
4545
*
46-
* Limits the string based on the character count. Preserves complete words
46+
* Limits the string based on the character count. Preserves complete words
4747
* so the character count may not be exactly as specified.
4848
*
4949
* @param string $endChar the end character. Usually an ellipsis
5050
*/
51-
function character_limiter(string $str, int $n = 500, string $endChar = '…'): string
51+
function character_limiter(string $string, int $limit = 500, string $endChar = '…'): string
5252
{
53-
if (mb_strlen($str) < $n) {
54-
return $str;
53+
if (mb_strlen($string) < $limit) {
54+
return $string;
5555
}
5656

5757
// a bit complicated, but faster than preg_replace with \s+
58-
$str = preg_replace('/ {2,}/', ' ', str_replace(["\r", "\n", "\t", "\x0B", "\x0C"], ' ', $str));
58+
$string = preg_replace('/ {2,}/', ' ', str_replace(["\r", "\n", "\t", "\x0B", "\x0C"], ' ', $string));
59+
$stringLength = mb_strlen($string);
5960

60-
if (mb_strlen($str) <= $n) {
61-
return $str;
61+
if ($stringLength <= $limit) {
62+
return $string;
6263
}
6364

64-
$out = '';
65+
$output = '';
66+
$outputLength = 0;
67+
$words = explode(' ', trim($string));
6568

66-
foreach (explode(' ', trim($str)) as $val) {
67-
$out .= $val . ' ';
68-
if (mb_strlen($out) >= $n) {
69-
$out = trim($out);
69+
foreach ($words as $word) {
70+
$output .= $word . ' ';
71+
$outputLength = mb_strlen($output);
72+
73+
if ($outputLength >= $limit) {
74+
$output = trim($output);
7075
break;
7176
}
7277
}
7378

74-
return (mb_strlen($out) === mb_strlen($str)) ? $out : $out . $endChar;
79+
return ($outputLength === $stringLength) ? $output : $output . $endChar;
7580
}
7681
}
7782

@@ -721,9 +726,9 @@ function excerpt(string $text, ?string $phrase = null, int $radius = 100, string
721726
$beforeWords = explode(' ', mb_substr($text, 0, $phrasePosition));
722727
$afterWords = explode(' ', mb_substr($text, $phrasePosition + $phraseLength));
723728

724-
$firstPartOutput = ' ';
725-
$endPartOutput = ' ';
726-
$count = 0;
729+
$firstPartOutput = ' ';
730+
$endPartOutput = ' ';
731+
$count = 0;
727732

728733
foreach (array_reverse($beforeWords) as $beforeWord) {
729734
$beforeWordLength = mb_strlen($beforeWord);

user_guide_src/source/helpers/text_helper.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ The following functions are available:
166166

167167
.. literalinclude:: text_helper/012.php
168168

169-
.. php:function:: word_limiter($str[, $limit = 100[, $end_char = '&#8230;']])
169+
.. php:function:: word_limiter($str[, $limit = 100[, $endChar = '&#8230;']])
170170
171171
:param string $str: Input string
172172
:param int $limit: Limit
173-
:param string $end_char: End character (usually an ellipsis)
173+
:param string $endChar: End character (usually an ellipsis)
174174
:returns: Word-limited string
175175
:rtype: string
176176

@@ -181,11 +181,11 @@ The following functions are available:
181181
The third parameter is an optional suffix added to the string. By
182182
default it adds an ellipsis.
183183

184-
.. php:function:: character_limiter($str[, $n = 500[, $end_char = '&#8230;']])
184+
.. php:function:: character_limiter($string[, $limit = 500[, $endChar = '&#8230;']])
185185
186-
:param string $str: Input string
187-
:param int $n: Number of characters
188-
:param string $end_char: End character (usually an ellipsis)
186+
:param string $string: Input string
187+
:param int $limit: Number of characters
188+
:param string $endChar: End character (usually an ellipsis)
189189
:returns: Character-limited string
190190
:rtype: string
191191

0 commit comments

Comments
 (0)