diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index a9ff9aae2133..44bbf7918ead 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -625,15 +625,28 @@ public static function length($value, $encoding = null) * @param string $value * @param int $limit * @param string $end + * @param bool $preserveWords * @return string */ - public static function limit($value, $limit = 100, $end = '...') + public static function limit($value, $limit = 100, $end = '...', $preserveWords = false) { if (mb_strwidth($value, 'UTF-8') <= $limit) { return $value; } - return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end; + if (! $preserveWords) { + return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end; + } + + $value = trim(preg_replace('/[\n\r]+/', ' ', strip_tags($value))); + + $trimmed = rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')); + + if (mb_substr($value, $limit, 1, 'UTF-8') === ' ') { + return $trimmed.$end; + } + + return preg_replace("/(.*)\s.*/", '$1', $trimmed).$end; } /** diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index c6953e939bce..be4389d523eb 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -433,11 +433,12 @@ public function length($encoding = null) * * @param int $limit * @param string $end + * @param bool $preserveWords * @return static */ - public function limit($limit = 100, $end = '...') + public function limit($limit = 100, $end = '...', $preserveWords = false) { - return new static(Str::limit($this->value, $limit, $end)); + return new static(Str::limit($this->value, $limit, $end, $preserveWords)); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 15e142574b3f..813dccdbd94b 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -596,15 +596,22 @@ public function testLimit() { $this->assertSame('Laravel is...', Str::limit('Laravel is a free, open source PHP web application framework.', 10)); $this->assertSame('这是一...', Str::limit('这是一段中文', 6)); + $this->assertSame('Laravel is a...', Str::limit('Laravel is a free, open source PHP web application framework.', 15, preserveWords: true)); $string = 'The PHP framework for web artisans.'; $this->assertSame('The PHP...', Str::limit($string, 7)); + $this->assertSame('The PHP...', Str::limit($string, 10, preserveWords: true)); $this->assertSame('The PHP', Str::limit($string, 7, '')); + $this->assertSame('The PHP', Str::limit($string, 10, '', true)); $this->assertSame('The PHP framework for web artisans.', Str::limit($string, 100)); + $this->assertSame('The PHP framework for web artisans.', Str::limit($string, 100, preserveWords: true)); + $this->assertSame('The PHP framework...', Str::limit($string, 20, preserveWords: true)); $nonAsciiString = '这是一段中文'; $this->assertSame('这是一...', Str::limit($nonAsciiString, 6)); + $this->assertSame('这是一...', Str::limit($nonAsciiString, 6, preserveWords: true)); $this->assertSame('这是一', Str::limit($nonAsciiString, 6, '')); + $this->assertSame('这是一', Str::limit($nonAsciiString, 6, '', true)); } public function testLength()