[ticket/15403] Remove last element of array only if needed

PHPBB-15403
This commit is contained in:
Ruben Calvo 2024-07-15 12:30:10 +02:00
parent d679ac4df8
commit 06ff814f04
No known key found for this signature in database

View file

@ -368,20 +368,16 @@ function get_context(string $text, array $words, int $length = 400): string
$end = $start + $characters_per_word; $end = $start + $characters_per_word;
// Check if we can merge this fragment into the previous fragment // Check if we can merge this fragment into the previous fragment
$last_element = array_pop($fragments); if (!empty($fragments))
if ($last_element !== null)
{ {
[$prev_start, $prev_end] = $last_element; [$prev_start, $prev_end] = end($fragments);
if ($prev_end + $characters_per_word >= $index + $word_length) if ($prev_end + $characters_per_word >= $index + $word_length)
{ {
array_pop($fragments);
$start = $prev_start; $start = $prev_start;
$end = $prev_end + $characters_per_word; $end = $prev_end + $characters_per_word;
} }
else
{
$fragments[] = $last_element;
}
} }
$fragments[] = [$start, $end]; $fragments[] = [$start, $end];
@ -419,7 +415,7 @@ function get_context(string $text, array $words, int $length = 400): string
$output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1); $output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1);
} }
return ($fragments[0][0] != 0 ? '... ' : '') . htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : ''); return ($fragments[0][0] !== 0 ? '... ' : '') . htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : '');
} }
/** /**