From 841600867de41231c47a7796b2b4e9d39ceeee92 Mon Sep 17 00:00:00 2001 From: forevermatt Date: Thu, 27 Oct 2016 12:36:40 -0400 Subject: [PATCH] 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. --- src/Stringy.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Stringy.php b/src/Stringy.php index 3f3592f..4641865 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -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;