mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/15403] Remove last element of array only if needed
PHPBB-15403
This commit is contained in:
parent
d679ac4df8
commit
06ff814f04
1 changed files with 4 additions and 8 deletions
|
@ -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 ? ' ...' : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue