Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Toolkit/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,11 @@ public static function excerpt(
if ($strip === true) {
// ensure that opening tags are preceded by a space, so that
// when tags are skipped we can be sure that words stay separate
$string = preg_replace('#\s*<([^\/])#', ' <${1}', $string);
// but only if there's a word character directly before it
$string = preg_replace('#(\w)<([^/][^>]*)>#', '${1} <${2}>', $string);

// add space after closing tag if there's a word character directly after it
$string = preg_replace('#</([^>]+)>(\w)#', '</${1}> ${2}', $string);

// in strip mode, we always return plain text
$string = strip_tags($string);
Expand Down
21 changes: 21 additions & 0 deletions tests/Toolkit/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ public function testExcerptWithTagFollowedByInterpunctuation(): void
$this->assertSame($expected, $result);
}

public function testExcerptWithWordCharactersAroundTags(): void
{
// Test case from issue #7306: word characters directly adjacent to tags
$string = 'A link in the middle of a word: Ultra<a href="/">Link</a>Modern.';
$expected = 'A link in the middle of a word: Ultra Link Modern.';
$result = Str::excerpt($string, 200);
$this->assertSame($expected, $result);

// Test brackets around tags (should not add extra spaces)
$string = '[<a href="/">Link</a>]';
$expected = '[Link]';
$result = Str::excerpt($string, 200);
$this->assertSame($expected, $result);

// Test parentheses around tags (should not add extra spaces)
$string = '(<a href="/">Link</a>)';
$expected = '(Link)';
$result = Str::excerpt($string, 200);
$this->assertSame($expected, $result);
}

public function testFloat(): void
{
$this->assertSame('0', Str::float(false));
Expand Down