diff --git a/docs/helpers/text_helper.md b/docs/helpers/text_helper.md index 826b87188e8c..162ba2e83e1a 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, the excerpt will include the first `$radius` characters 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..e83b39a647b5 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -450,6 +450,24 @@ public function testExcerptRadius(): void $this->assertSame($multibyteResult, excerpt($this->mbLongString, 'печально', 10)); } + public function testExcerptPhraseNotFound(): void + { + $result = ' 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. ...'; + $this->assertSame($result, excerpt($this->longString, 'nonexistent')); + + $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; + $this->assertSame($multibyteResult, excerpt($this->mbLongString, 'несуществующее')); + } + + public function testExcerptOddRadius(): void + { + $result = ' 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. ...'; + $this->assertSame($result, excerpt($this->longString, null, 101)); + + $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; + $this->assertSame($multibyteResult, excerpt($this->mbLongString, null, 101)); + } + public function testAlternator(): void { $phrase = ' scream! ';