diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 367c69f9cb37..d393b5a91982 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -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. diff --git a/docs/helpers/text_helper.md b/docs/helpers/text_helper.md index 826b87188e8c..c17223f82de4 100644 --- a/docs/helpers/text_helper.md +++ b/docs/helpers/text_helper.md @@ -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: diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index 09d14abbe2ee..20abc3d5f7de 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -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); + } } else { - $phrasePosition = $radius / 2; + $phrasePosition = intdiv($radius, 2); $phraseLength = 1; } diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index 88878bcf8294..ec81c921c097 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -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! ';