Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d33a483

Browse filesBrowse files
committed
feature #35649 [String] Allow to keep the last word when truncating a text (franmomu)
This PR was merged into the 5.1-dev branch. Discussion ---------- [String] Allow to keep the last word when truncating a text | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #35567 | License | MIT | Doc PR | - The [truncate filter from twig/extensions](https://github.com/twigphp/Twig-extensions/blob/master/src/TextExtension.php#L36) has a `preserve` parameter to preserve whole words. Since `twig/extensions` is deprecated and its alternative for `truncate` filter is the use of `u` filter, this PR adds preverse word functionality. Commits ------- 1cfaeec [String] Allow to keep the last word when truncating a text
2 parents cde44fc + 1cfaeec commit d33a483
Copy full SHA for d33a483

File tree

3 files changed

+16
-6
lines changed
Filter options

3 files changed

+16
-6
lines changed

‎src/Symfony/Component/String/AbstractString.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/String/AbstractString.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FE
620620
/**
621621
* @return static
622622
*/
623-
public function truncate(int $length, string $ellipsis = ''): self
623+
public function truncate(int $length, string $ellipsis = '', bool $cut = true): self
624624
{
625625
$stringLength = $this->length();
626626

@@ -634,6 +634,10 @@ public function truncate(int $length, string $ellipsis = ''): self
634634
$ellipsisLength = 0;
635635
}
636636

637+
if (!$cut) {
638+
$length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength);
639+
}
640+
637641
$str = $this->slice(0, $length - $ellipsisLength);
638642

639643
return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;

‎src/Symfony/Component/String/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/String/CHANGELOG.md
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ CHANGELOG
77
* added the `AbstractString::reverse()` method
88
* made `AbstractString::width()` follow POSIX.1-2001
99
* added `LazyString` which provides memoizing stringable objects
10-
* The component is not marked as `@experimental` anymore.
11-
* Added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
12-
depending of the input string UTF-8 compliancy.
10+
* The component is not marked as `@experimental` anymore
11+
* added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
12+
depending of the input string UTF-8 compliancy
13+
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`
1314

1415
5.0.0
1516
-----

‎src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,9 +1405,9 @@ public static function providePadStart()
14051405
/**
14061406
* @dataProvider provideTruncate
14071407
*/
1408-
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis)
1408+
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $cut = true)
14091409
{
1410-
$instance = static::createFromString($origin)->truncate($length, $ellipsis);
1410+
$instance = static::createFromString($origin)->truncate($length, $ellipsis, $cut);
14111411

14121412
$this->assertEquals(static::createFromString($expected), $instance);
14131413
}
@@ -1417,12 +1417,17 @@ public static function provideTruncate()
14171417
return [
14181418
['', '', 3, ''],
14191419
['', 'foo', 0, '...'],
1420+
['foo', 'foo', 0, '...', false],
14201421
['fo', 'foobar', 2, ''],
14211422
['foobar', 'foobar', 10, ''],
1423+
['foobar', 'foobar', 10, '...', false],
14221424
['foo', 'foo', 3, '...'],
14231425
['fo', 'foobar', 2, '...'],
14241426
['...', 'foobar', 3, '...'],
14251427
['fo...', 'foobar', 5, '...'],
1428+
['foobar...', 'foobar foo', 6, '...', false],
1429+
['foobar...', 'foobar foo', 7, '...', false],
1430+
['foobar foo...', 'foobar foo a', 10, '...', false],
14261431
];
14271432
}
14281433

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.