Skip to content
Open
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
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, the excerpt will include the first `$radius` characters 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
18 changes: 18 additions & 0 deletions tests/system/Helpers/TextHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. ...';
Comment thread
banghamid marked this conversation as resolved.
Resolved
$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! ';
Expand Down
Loading