From 06ff814f047836a929effa217af5a60f89e8da40 Mon Sep 17 00:00:00 2001 From: Ruben Calvo Date: Mon, 15 Jul 2024 12:30:10 +0200 Subject: [PATCH] [ticket/15403] Remove last element of array only if needed PHPBB-15403 --- phpBB/includes/functions_content.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index bb2ac3e8a7..2950fe762a 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -368,20 +368,16 @@ function get_context(string $text, array $words, int $length = 400): string $end = $start + $characters_per_word; // Check if we can merge this fragment into the previous fragment - $last_element = array_pop($fragments); - if ($last_element !== null) + if (!empty($fragments)) { - [$prev_start, $prev_end] = $last_element; + [$prev_start, $prev_end] = end($fragments); if ($prev_end + $characters_per_word >= $index + $word_length) { + array_pop($fragments); $start = $prev_start; $end = $prev_end + $characters_per_word; } - else - { - $fragments[] = $last_element; - } } $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); } - return ($fragments[0][0] != 0 ? '... ' : '') . htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : ''); + return ($fragments[0][0] !== 0 ? '... ' : '') . htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : ''); } /**