', '<', '(', ')', '~', '*', '"'); protected static $_highlightTemplate = '$1'; public static function markSearchWords($body, $searchText) { if (!$searchText) { return $body; } $body = preg_replace('/\s+/', ' ', $body); $wordList = Qs_Text_Mark::getWordList($searchText); foreach ($wordList as $word) { $block = Qs_Text_Mark::getMarkedBlock($body, $word); if ($block !== false){ break; } } if (empty($block)){ $block = substr($body, 0, Qs_Text_Mark::$_charactersBefore + Qs_Text_Mark::$_charactersAfter); if (strlen($block) < strlen($body)) { $block .= '...'; } } $block = Qs_Text_Mark::markSearchWord($block, $wordList); return $block; } public static function getWordList($searchText) { $words = explode(' ', trim(str_replace(Qs_Text_Mark::$_wordSplitters, ' ', $searchText))); return array_unique($words); } public static function getMarkedBlock($body, $searchText) { if (!empty(Qs_Text_Mark::$_charactersBefore) && !empty(Qs_Text_Mark::$_charactersAfter)) { $bodyLowercase = strtolower($body); $searchTextLowercase = strtolower($searchText); $searchTextStart = strpos($bodyLowercase, $searchTextLowercase); if ($searchTextStart !== false){ $blockStart = max($searchTextStart - Qs_Text_Mark::$_charactersBefore, 0); if ($blockStart > 0 && false !== ($nextSpacePos = strpos($body, ' ', $blockStart))) { $blockStart = $nextSpacePos + 1; } $blockLength = $searchTextStart - $blockStart + strlen($searchText) + Qs_Text_Mark::$_charactersAfter; if (strlen($body) > $blockStart + $blockLength && false !== ($nextSpacePos = strpos($body, ' ', $blockStart + $blockLength)) ) { $blockLength = $nextSpacePos - $blockStart; } $block = ($blockStart > 0 ? '...' : '') . substr($body, $blockStart, $blockLength) . ($blockLength + $blockStart < strlen($body) ? '...' : ''); } else { $block = false; } } else { $block = $body; } return $block; } public static function markSearchWord($body, $words) { if (!is_array($words)) { $words = array($words); } foreach ($words as &$word) { $word = preg_quote($word, '/'); } $result = preg_replace( array('/\s/', '/(' . implode('|', $words) . ')/i'), array(' ', Qs_Text_Mark::$_highlightTemplate), $body ); return $result; } public static function setCharactersBefore($charactersBefore) { Qs_Sys_Text_Mark::$_charactersBefore = $charactersBefore; return true; } public static function setCharactersAfter($charactersAfter) { Qs_Sys_Text_Mark::$_charactersAfter = $charactersAfter; return true; } public static function setWordSplitters($wordSplitters) { Qs_Sys_Text_Mark::$_wordSplitters = $wordSplitters; return true; } public static function addSplitter($splitter) { if (!in_array(Qs_Sys_Text_Mark::$_wordSplitters, $splitter)) { Qs_Sys_Text_Mark::$_wordSplitters[] = $splitter; } return true; } public static function removeSplitter($splitter) { if (in_array(Qs_Sys_Text_Mark::$_wordSplitters, $splitter)) { unset(Qs_Sys_Text_Mark::$_wordSplitters[array_search($splitter, Qs_Sys_Text_Mark::$_wordSplitters)]); } return true; } public static function setHighlightTemplate($highlightTemplate) { Qs_Sys_Text_Mark::$_highlightTemplate = $highlightTemplate; return true; } }