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
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Release Date: Unreleased
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
- **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas.
- **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters.
- **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`.
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
- **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format.
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/text_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ this_string_is_e…ak_my_design.jpg

This function will extract `$radius` number of characters before and after the central `$phrase` with an ellipsis before and after.

The first parameter is the text to extract an excerpt from, and the second is the central word or phrase to count before and after. The third parameter is the number of characters to count before and after the central phrase. If no phrase is passed, the excerpt will include the first `$radius` characters with the ellipsis at the end.
The first parameter is the text to extract an excerpt from, and the second is the central word or phrase to count before and after. The third parameter is the number of characters to count before and after the central phrase. If no phrase is passed, or if the phrase is not found in the text, it falls back to extracting an excerpt of whole words from the beginning of the text based on the radius with the ellipsis at the end.

Example:

Expand Down
10 changes: 7 additions & 3 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,16 +709,20 @@ function alternator(...$args): string
* @param int $radius The amount of characters returned around the phrase.
* @param string $ellipsis Ending that will be appended
*
* If no $phrase is passed, will generate an excerpt of $radius characters
* from the beginning of $text.
* If no $phrase is passed or $phrase is not found, will generate an excerpt
* of $radius characters from the beginning of $text.
*/
function excerpt(string $text, ?string $phrase = null, int $radius = 100, string $ellipsis = '...'): string
{
if (isset($phrase)) {
$phrasePosition = mb_stripos($text, $phrase);
$phraseLength = mb_strlen($phrase);

if ($phrasePosition === false) {
return excerpt($text, null, $radius, $ellipsis);
Comment thread
banghamid marked this conversation as resolved.
Resolved
}
} else {
$phrasePosition = $radius / 2;
$phrasePosition = intdiv($radius, 2);
$phraseLength = 1;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/system/Helpers/TextHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,30 @@ public function testExcerptRadius(): void
$this->assertSame($multibyteResult, excerpt($this->mbLongString, 'печально', 10));
}

public function testExcerptPhraseNotFound(): void
{
$this->assertSame(
' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...',
excerpt($this->longString, 'nonexistent'),
);
$this->assertSame(
' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...',
excerpt($this->mbLongString, 'несуществующее'),
);
}

public function testExcerptOddRadius(): void
{
$this->assertSame(
' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...',
excerpt($this->longString, null, 101),
);
$this->assertSame(
' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...',
excerpt($this->mbLongString, null, 101),
);
}

public function testAlternator(): void
{
$phrase = ' scream! ';
Expand Down
Loading