Skip to content

Commit

Permalink
Merge pull request #46 from JAK0TA/add-method-to-keep-lines
Browse files Browse the repository at this point in the history
feat: add method to reduce text length
  • Loading branch information
tlueder authored Dec 7, 2023
2 parents 5653569 + 02b57fe commit c049885
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Classes/Utility/TextUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,25 @@ public static function calculateReadingTime(string $text): \stdClass {

return $stats;
}

/**
* Trims a multiline string to a specified number of lines.
*
* This function takes a string and an optional line limit. It then trims the
* string so that it contains no more than the specified number of lines. If the
* string has fewer lines than the limit, it remains unchanged. The function ensures
* that the output always ends with a newline character.
*
* @param string $string the multiline string to be trimmed
* @param int $lines Optional. The maximum number of lines to keep in the string.
* Defaults to 100.
*
* @return string the trimmed string, containing no more than the specified number of lines
*/
public static function keepLines(string $string, int $lines = 100): string {
$string = explode("\n", $string);
array_splice($string, $lines);

return implode("\n", $string)."\n";
}
}

0 comments on commit c049885

Please sign in to comment.