src/Utils/Excerpt.php
/**
     * Work out which is the most relevant portion to display
     * This is done by looping over each match and finding the smallest distance between two found
     * strings. The idea being that the closer the terms are the better match the snippet would be.
     * When checking for matches we only change the location if there is a better match.
     * The only exception is where we have only two matches in which case we just take the
     * first as will be equally distant.
     */
Work out which is the most relevant portion to display
This is done by looping over each match and finding the smallest distance between two found
strings. The idea being that the closer the terms are the better match the snippet would be.
When checking for matches we only change the location if there is a better match.
The only exception is where we have only two matches in which case we just take the
first as will be equally distant.
Work out which is the most relevant portion to display This is done by looping over each match and finding the smallest distance between two found strings . The idea being that the closer the terms are the better match the snippet would be . When checking for matches we only change the location if there is a better match . The only exception is where we have only two matches in which case we just take the first as will be equally distant .
protected static function determineSnipLocation(array $locations, int $prevCount): int
    {
        $startPos = (int) reset($locations);
        $locCount = count($locations);
        $smallestDiff = PHP_INT_MAX;
        if ($locCount > 2) {
            for ($i = 1; $i < $locCount; ++$i) {
                if ($i === $locCount - 1) {
                    $diff = $locations[$i] - $locations[$i - 1];
                } else {
                    $diff = $locations[$i + 1] - $locations[$i];
                }
                if ($smallestDiff > $diff) {
                    $smallestDiff = $diff;
                    $startPos = $locations[$i];
                }
            }
        }
        return $startPos > $prevCount ? $startPos - $prevCount : 0;
    }
protected static function determineSnipLocation ( array $ locations , int $ prevCount ) : int { $ startPos = ( int ) reset ( $ locations ) ; $ locCount = count ( $ locations ) ; $ smallestDiff = PHP_INT_MAX ; if ( $ locCount > 2 ) { for ( $ i = 1 ; $ i < $ locCount ; ++ $ i ) { if ( $ i === $ locCount - 1 ) { $ diff = $ locations [ $ i ] - $ locations [ $ i - 1 ] ; } else { $ diff = $ locations [ $ i + 1 ] - $ locations [ $ i ] ; } if ( $ smallestDiff > $ diff ) { $ smallestDiff = $ diff ; $ startPos = $ locations [ $ i ] ; } } } return $ startPos > $ prevCount ? $ startPos - $ prevCount : 0 ; }
Work out which is the most relevant portion to display
 This is done by looping over each match and finding the smallest distance between two found
 strings.
Work out which is the most relevant portion to display This is done by looping over each match and finding the smallest distance between two found strings .
// If we only have 1 match we don't actually do the for loop so set to the first // If we only have 2, skip as it's probably equally relevant // skip the first as we check 1 behind // at the end