Skip to content

Commit

Permalink
Added completeWords flag to limit str method (#52245)
Browse files Browse the repository at this point in the history
* Added completeWords flag to limit str method

* formatting

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
itsmewes and taylorotwell committed Jul 23, 2024
1 parent 82f9d3d commit b18f333
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b18f333

Please sign in to comment.