<?php

class Helpers
{
    public static function HighlightQuery($query, $text, $start = '<mark>', $end = '</mark>')
    {
        $skip = strlen($start) + strlen($end);
        $words = explode(' ', str_replace(['+', '-', 'AND', 'OR', 'NOT', '"', '\''], [], $query));
        $result = $text;
        foreach($words as $word) {
            $startAt = 0;
            $len = strlen($word);
            while(($startAt < strlen($result)) && (($index = stripos($result, $word, $startAt)) !== false)) {
                $result = substr($result, 0, $index) . $start . substr($result, $index, $len) . $end . substr($result, $index + $len);
                $startAt = $index + $skip;
            } 
        }

        return $result;
    }
};
