Skip to content

Commit b18f333

Browse files
Added completeWords flag to limit str method (#52245)
* Added completeWords flag to limit str method * formatting * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 82f9d3d commit b18f333

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/Illuminate/Support/Str.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,28 @@ public static function length($value, $encoding = null)
625625
* @param string $value
626626
* @param int $limit
627627
* @param string $end
628+
* @param bool $preserveWords
628629
* @return string
629630
*/
630-
public static function limit($value, $limit = 100, $end = '...')
631+
public static function limit($value, $limit = 100, $end = '...', $preserveWords = false)
631632
{
632633
if (mb_strwidth($value, 'UTF-8') <= $limit) {
633634
return $value;
634635
}
635636

636-
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
637+
if (! $preserveWords) {
638+
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
639+
}
640+
641+
$value = trim(preg_replace('/[\n\r]+/', ' ', strip_tags($value)));
642+
643+
$trimmed = rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8'));
644+
645+
if (mb_substr($value, $limit, 1, 'UTF-8') === ' ') {
646+
return $trimmed.$end;
647+
}
648+
649+
return preg_replace("/(.*)\s.*/", '$1', $trimmed).$end;
637650
}
638651

639652
/**

src/Illuminate/Support/Stringable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,12 @@ public function length($encoding = null)
433433
*
434434
* @param int $limit
435435
* @param string $end
436+
* @param bool $preserveWords
436437
* @return static
437438
*/
438-
public function limit($limit = 100, $end = '...')
439+
public function limit($limit = 100, $end = '...', $preserveWords = false)
439440
{
440-
return new static(Str::limit($this->value, $limit, $end));
441+
return new static(Str::limit($this->value, $limit, $end, $preserveWords));
441442
}
442443

443444
/**

tests/Support/SupportStrTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,15 +596,22 @@ public function testLimit()
596596
{
597597
$this->assertSame('Laravel is...', Str::limit('Laravel is a free, open source PHP web application framework.', 10));
598598
$this->assertSame('这是一...', Str::limit('这是一段中文', 6));
599+
$this->assertSame('Laravel is a...', Str::limit('Laravel is a free, open source PHP web application framework.', 15, preserveWords: true));
599600

600601
$string = 'The PHP framework for web artisans.';
601602
$this->assertSame('The PHP...', Str::limit($string, 7));
603+
$this->assertSame('The PHP...', Str::limit($string, 10, preserveWords: true));
602604
$this->assertSame('The PHP', Str::limit($string, 7, ''));
605+
$this->assertSame('The PHP', Str::limit($string, 10, '', true));
603606
$this->assertSame('The PHP framework for web artisans.', Str::limit($string, 100));
607+
$this->assertSame('The PHP framework for web artisans.', Str::limit($string, 100, preserveWords: true));
608+
$this->assertSame('The PHP framework...', Str::limit($string, 20, preserveWords: true));
604609

605610
$nonAsciiString = '这是一段中文';
606611
$this->assertSame('这是一...', Str::limit($nonAsciiString, 6));
612+
$this->assertSame('这是一...', Str::limit($nonAsciiString, 6, preserveWords: true));
607613
$this->assertSame('这是一', Str::limit($nonAsciiString, 6, ''));
614+
$this->assertSame('这是一', Str::limit($nonAsciiString, 6, '', true));
608615
}
609616

610617
public function testLength()

0 commit comments

Comments
 (0)