예제 #1
0
 function word_highlight($body, $q_word, $enc = null, $msg = '', $extlink_class_name = 'ext', $word_max_len = 20)
 {
     if (is_null($enc)) {
         if (function_exists('mb_internal_encoding')) {
             $enc = mb_internal_encoding();
         } else {
             if (defined('_CHARSET')) {
                 $enc = _CHARSET;
             } else {
                 $enc = 'EUC-JP';
             }
         }
     }
     $enc = strtoupper($enc);
     // 外部リンクの場合 class="ext" を付加
     if ($extlink_class_name) {
         $_body = preg_replace_callback('/(<script.*?<\\/script>)|(<a[^>]+?href=(?:"|\')?(?!https?:\\/\\/' . $_SERVER['HTTP_HOST'] . ')https?:[^>]+?)>/isS', create_function('$arr', 'return $arr[1]? $arr[1] : ((strpos($arr[2], \'class=\') === FALSE)? "$arr[2] class=\\"' . $extlink_class_name . '\\">" : "$arr[0]");'), $body);
         if ($_body) {
             $body = $_body;
         }
         // for RCRE error.
     }
     if (!$q_word || !$body) {
         return $body;
     }
     if (function_exists("xoops_gethandler")) {
         $config_handler =& xoops_gethandler('config');
         $xoopsConfigSearch =& $config_handler->getConfigsByCat(XOOPS_CONF_SEARCH);
     } else {
         $xoopsConfigSearch['keyword_min'] = 3;
     }
     //検索語ハイライト
     $search_word = '';
     //$words = array_flip(preg_split('/\s+/',$q_word,-1,PREG_SPLIT_NO_EMPTY));
     $words = array_flip(HypCommonFunc::phrase_split($q_word));
     $keys = array();
     $cnt = 0;
     if (function_exists('mb_strlen')) {
         $strlen = create_function('$str', 'return mb_strlen("$str");');
     } else {
         $strlen = create_function('$str', 'return strlen("$str");');
     }
     foreach ($words as $word => $id) {
         $_len = $strlen($word);
         if ($_len < $xoopsConfigSearch['keyword_min']) {
             continue;
         }
         if ($_len > $word_max_len) {
             continue;
         }
         $keys[$word] = $strlen($word);
         $cnt++;
         if ($cnt > 10) {
             break;
         }
     }
     //arsort($keys,SORT_NUMERIC);
     $keys = HypGetQueryWord::get_search_words(array_keys($keys), false, $enc);
     $id = 0;
     $utf8 = $enc === 'UTF-8' ? 'u' : '';
     $php5_1 = version_compare(PHP_VERSION, '5.1.0', '>=');
     foreach ($keys as $key => $pattern) {
         $s_key = preg_replace('/&amp;#(\\d+;)/', '&#$1', htmlspecialchars($key));
         $_count = 0;
         $pattern = $s_key[0] == '&' ? '/(<head.*?<\\/head>|<script.*?<\\/script>|<style.*?<\\/style>|<textarea.*?<\\/textarea>|<strong class="word\\d+">.*?<\\/strong>|<[^>]*>)|(' . $pattern . ')/isS' . $utf8 : '/(<head.*?<\\/head>|<script.*?<\\/script>|<style.*?<\\/style>|<textarea.*?<\\/textarea>|<strong class="word\\d+">.*?<\\/strong>|<[^>]*>|&(?:#[0-9]+|#x[0-9a-f]+|[0-9a-zA-Z]+);)|(' . $pattern . ')/isS' . $utf8;
         $GLOBALS['HypGetQueryWord_Highlighted'] = false;
         $_body = preg_replace_callback($pattern, create_function('$arr', 'if ($arr[1]) {return $arr[1];} else {$GLOBALS[\'HypGetQueryWord_Highlighted\'] = true; return \'<strong class="word' . $id . '">\'.$arr[2].\'</strong>\';}'), $body);
         if ($GLOBALS['HypGetQueryWord_Highlighted']) {
             $body = $_body;
             $search_word .= ' <strong class="word' . $id . '">' . $s_key . '</strong>';
             $id++;
         }
     }
     if ($id) {
         $_count = 0;
         $body = str_replace('<!--HIGHLIGHT_SEARCH_WORD-->', '<div class="highlight_search_words">' . $msg . ': ' . $search_word . '</div>', $body, $_count);
         if (!$_count) {
             $body = preg_replace('/<body[^>]*?>/', '$0<div class="highlight_search_words">' . str_replace('\\', '\\\\', $msg) . ': ' . str_replace('\\', '\\\\', $search_word) . '</div>', $body);
         }
     }
     return $body;
 }
예제 #2
0
 function wordsHighlight($s)
 {
     if ($s === '' || strpos($s, '<html') === FALSE) {
         return false;
     }
     if (function_exists('mb_convert_encoding') && $this->configEncoding && $this->encode !== $this->configEncoding) {
         $this->msg_words_highlight = mb_convert_encoding($this->msg_words_highlight, $this->encode, $this->configEncoding);
     }
     $ret = HypGetQueryWord::word_highlight($s, defined($this->q_word2) ? constant($this->q_word) . ' ' . constant($this->q_word2) : constant($this->q_word), $this->encode, $this->msg_words_highlight, $this->extlink_class_name);
     if (!$s || strlen($s) === $ret) {
         return false;
     }
     $this->changeContentLength = true;
     return $ret;
 }