mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/15403] Use substr over the fragment and not the whole text
PHPBB-15403
This commit is contained in:
parent
5f0e639b83
commit
ec8bbf3da3
1 changed files with 5 additions and 4 deletions
|
@ -399,23 +399,24 @@ function get_context(string $text, array $words, int $length = 400): string
|
|||
{
|
||||
$fragment = utf8_substr($text, $start, $end - $start + 1);
|
||||
|
||||
$offset = $start;
|
||||
$fragment_start = 0;
|
||||
$fragment_end = $end - $start + 1;
|
||||
|
||||
// Find the first valid alphanumeric character in the fragment to don't cut words
|
||||
if ($start > 0)
|
||||
{
|
||||
preg_match('/[^a-zA-Z0-9][a-zA-Z0-9]/u', $fragment, $matches, PREG_OFFSET_CAPTURE);
|
||||
$start = $offset + (int) $matches[0][1] + 1; // first valid alphanumeric character
|
||||
$fragment_start = (int) $matches[0][1] + 1; // first valid alphanumeric character
|
||||
}
|
||||
|
||||
// Find the last valid alphanumeric character in the fragment to don't cut words
|
||||
if ($end < $text_length - 1)
|
||||
{
|
||||
preg_match_all('/[a-zA-Z0-9][^a-zA-Z0-9]/u', $fragment, $matches, PREG_OFFSET_CAPTURE);
|
||||
$end = $offset + end($matches[0])[1]; // last valid alphanumeric character
|
||||
$fragment_end = end($matches[0])[1]; // last valid alphanumeric character
|
||||
}
|
||||
|
||||
$output[] = utf8_substr($text, $start, $end - $start + 1);
|
||||
$output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1);
|
||||
}
|
||||
|
||||
return htmlentities(implode(' ... ', $output)) . ($end < $text_length - 1 ? ' ...' : '');
|
||||
|
|
Loading…
Add table
Reference in a new issue