1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-10 23:34:00 +02:00

Fix safeTruncate to handle strings without spaces and those where the first word is longer than the desired truncation length

Before this, calling safeTruncate with a string without spaces and a truncate length less than the string length would return an empty string.
This commit is contained in:
forevermatt
2016-10-27 12:36:40 -04:00
committed by Daniel St. Jules
parent 845e8d693d
commit 841600867d

View File

@@ -1123,7 +1123,9 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess
if (mb_strpos($stringy->str, ' ', $length - 1, $encoding) != $length) {
// Find pos of the last occurrence of a space, get up to that
$lastPos = \mb_strrpos($truncated, ' ', 0, $encoding);
$truncated = \mb_substr($truncated, 0, $lastPos, $encoding);
if ($lastPos !== false) {
$truncated = \mb_substr($truncated, 0, $lastPos, $encoding);
}
}
$stringy->str = $truncated . $substring;