From 5c40766dc4d9f5ca7bca3f87c94a15867e0a1069 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 6a360fc4a2..2e52863ef0 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -369,20 +369,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]; @@ -420,7 +416,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 ? ' ...' : ''); } /**