Skip to content

Commit

Permalink
Add Str::limit method
Browse files Browse the repository at this point in the history
This method truncates the given string to the specified length.
Use cases are long titles and descriptions in text shown to the user,
that would overload the screen.

Example:

```
$title = Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 10);

$title == 'Lorem ipsu...';
```
  • Loading branch information
martialblog committed Jul 12, 2024
1 parent bf5fc8f commit 3e46981
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,27 @@ public static function trimSplit(?string $subject, string $delimiter = ',', int

return array_map('trim', $exploded);
}

/**
* Str::limit method truncates the given string to the specified length.
*
* This method will replace the truncated part with an ellipsis.
*
* @param string $subject
* @param ?int $limit Length of the string that will be returned
* @param ?string $end Ellipsis that will be added to the truncated string
*
* @return string
*/
public static function limit(string $subject, ?int $limit = 50, ?string $end = '...'): string
{
// If the string is smaller or equal to the limit we simply return it
if (mb_strwidth($subject, 'UTF-8') <= $limit) {
return $subject;
}

// If the string is longer than the limit we truncate it
// and add the given end to it.
return mb_strimwidth($subject, 0, $limit, '', 'UTF-8') . $end;

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.

Check failure on line 114 in src/Str.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #3 $width of function mb_strimwidth expects int, int|null given.
}
}
16 changes: 16 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,20 @@ public function testTrimSplitRespectsLimit()
{
$this->assertSame(['foo', 'bar , baz'], Str::trimSplit(' foo ,bar , baz ', ',', 2));
}

public function testLimitWithSmallerString()
{
$this->assertSame('', Str::limit(''));
$this->assertSame('noop', Str::limit('noop'));
}

public function testLimitWithLongerStringAndSpecificLimit()
{
$this->assertSame('Lorem ipsu...', Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 10));
}

public function testLimitWithLongerStringAndSpecificEnd()
{
$this->assertSame('L (...)', Str::limit('Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 1, ' (...)'));
}
}

0 comments on commit 3e46981

Please sign in to comment.