Skip to content

Commit

Permalink
refactor: Rework character_limiter()
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Jan 6, 2025
1 parent c66bf2e commit 945e1bf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
37 changes: 21 additions & 16 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,40 @@ function word_limiter(string $str, int $limit = 100, string $endChar = '…'
/**
* Character Limiter
*
* Limits the string based on the character count. Preserves complete words
* Limits the string based on the character count. Preserves complete words
* so the character count may not be exactly as specified.
*
* @param string $endChar the end character. Usually an ellipsis
*/
function character_limiter(string $str, int $n = 500, string $endChar = '…'): string
function character_limiter(string $string, int $limit = 500, string $endChar = '…'): string
{
if (mb_strlen($str) < $n) {
return $str;
if (mb_strlen($string) < $limit) {
return $string;
}

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

if (mb_strlen($str) <= $n) {
return $str;
if ($stringLength <= $limit) {
return $string;
}

$out = '';
$output = '';
$outputLength = 0;
$words = explode(' ', trim($string));

foreach (explode(' ', trim($str)) as $val) {
$out .= $val . ' ';
if (mb_strlen($out) >= $n) {
$out = trim($out);
foreach ($words as $word) {
$output .= $word . ' ';
$outputLength = mb_strlen($output);

if ($outputLength >= $limit) {
$output = trim($output);
break;
}
}

return (mb_strlen($out) === mb_strlen($str)) ? $out : $out . $endChar;
return ($outputLength === $stringLength) ? $output : $output . $endChar;
}
}

Expand Down Expand Up @@ -722,9 +727,9 @@ function excerpt(string $text, ?string $phrase = null, int $radius = 100, string
$beforeWords = explode(' ', mb_substr($text, 0, $phrasePosition));
$afterWords = explode(' ', mb_substr($text, $phrasePosition + $phraseLength));

$firstPartOutput = ' ';
$endPartOutput = ' ';
$count = 0;
$firstPartOutput = ' ';
$endPartOutput = ' ';
$count = 0;

foreach (array_reverse($beforeWords) as $beforeWord) {
$beforeWordLength = mb_strlen($beforeWord);
Expand Down
12 changes: 6 additions & 6 deletions user_guide_src/source/helpers/text_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ The following functions are available:

.. literalinclude:: text_helper/012.php

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

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

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

Expand Down

0 comments on commit 945e1bf

Please sign in to comment.