突出显示mysql php搜索重音字符中的搜索词

时间:2014-02-12 04:37:44

标签: php mysql search

回答这个问题的答案3“Highlight search term in mysql php search”我可以实现单词突出显示,我唯一无法弄清楚的是如何突出单词的重音版本,查询确实找到了例如“wesha”和“weshá”但突出显示仅适用于“wesha”..

这是我的代码:

echo "<p>".str_replace($palabra,"<strong>$palabra</strong>",$row['definicion'])."</p>";

由于

顺便说一下,更改为str_ireplace会使资本中的单词匹配,但会将其更改为非大写字母,是否有办法将其包括在内?

2 个答案:

答案 0 :(得分:0)

这是由于数据库在进行搜索时正在进行音译。即如果你搜索'á',它就会获得'á'和'a'(translit)的匹配。您的应用程序代码需要对文本突出显示执行相同的音译。使用iconv实现此目标:http://in2.php.net/manual/en/function.iconv.php

答案 1 :(得分:0)

这是一个PHP类,它将通过利用PHP类Transliterator来突出显示某些HTML文本中搜索词的所有出现,该类自PHP 5.4起并且安装了intl扩展名。< / p>

此类将对HTML中的每个字符进行音译,然后对搜索词和音译HTML进行逐字符比较。它将使用提供的span的HTML $css_class元素突出显示匹配的字词。

此类还支持音译产生多个字符的字符,例如日语字符会音译为shou,因此如果搜索字词为手ld,字符should将在文本中突出显示。

该类仅受PHP Transliterator类实现的功能限制。

//------------------------------------------------------------------------------------------
// highlights all occurrences of an ascii $term_to_highlight in some
// $html string that may contain all sorts of weird characters
class SearchResultHighlighter {
//------------------------------------------------------------------------------------------
    public $term_to_highlight;
    protected $term_len;
    protected static $transliterator = null;

    //------------------------------------------------------------------------------------------
    public function __construct(
        $term_to_highlight,     // must be an already transliterated search term (ASCII only)
        $transliterator_rules   // rules passed to Transliterator::createFromRules
    ) {
        $this->term_to_highlight = $term_to_highlight;
        $this->term_len = mb_strlen($this->term_to_highlight);
        if(self::$transliterator === null) // Transliterator only available PHP >= 5.4.0, PECL intl >= 2.0.0
            self::$transliterator = class_exists('Transliterator') ? Transliterator::createFromRules($transliterator_rules) : null;
    }

    //------------------------------------------------------------------------------------------
    public function highlight(
        $html,              // the HTML in which to highlight all occurrences of $this->term_to_highlight
        $css_class = 'hl'   // the CSS class used to highlight occurrences
    ) {
        if(self::$transliterator === null)
            return $html;
        $result = '';
        $source_len = mb_strlen($html);
        $matched_term_chars = 0;
        $source_match_startpos = 0;
        $source_match_len = 0;
        for($i = 0; $i < $source_len; $i++) {
            $c = mb_substr($html, $i, 1);
            $c_trans = mb_strtolower(self::$transliterator->transliterate($c));
            $c_trans_len = mb_strlen($c_trans); // note: single transliterated chars can be more than one char, e.g. transliterate('手') yields 'shou'
            if($c_trans_len <= $this->term_len - $matched_term_chars && $c_trans === mb_substr($this->term_to_highlight, $matched_term_chars, $c_trans_len))    {
                if($matched_term_chars == 0)
                    $source_match_startpos = $i;
                $matched_term_chars += $c_trans_len;
                $source_match_len++;
                if($matched_term_chars == $this->term_len) {
                    $result .= sprintf('<span class="%s">%s</span>', $css_class, mb_substr($html, $source_match_startpos, $source_match_len));
                    $matched_term_chars = $source_match_len = 0;
                }
            }
            else {
                $result .= $source_match_len > 0 ? mb_substr($html, $source_match_startpos, $source_match_len + 1) : $c;
                $matched_term_chars = $source_match_startpos = $source_match_len = 0;
            }
        }
        return $result;
    }
}

例如,您可以按如下方式使用它。

$html = '<p>ŁoreM Ìpsum Ðolór. Šit Ämet. Some really long, accénted and diactritical stuff, e.g. the names Ḥasan or Abū ʿĀṣī come with some diacritics. James Bond loves Ms. Pussy Galore!</p>';

$transliteration_rules = ':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: Lower(); :: NFC;';

$highlighter = new SearchResultHighlighter('lore', $transliteration_rules);

echo $highlighter->highlight($html, 'yellow-bold');

(注意:有关音译规则的说明,请参阅PHP documentation of the Transliterator::createFromRules method) 这将产生。

<p><span class="yellow-bold">Łoré</span>M Ìpsum Ðolór. Šit Ämet. Some really long, accénted and diactritical stuff, e.g. the names Ḥasan or Abū ʿĀṣī come with some diacritics. James Bond loves Ms. Pussy Ga<span class="yellow-bold">lore</span>!</p>

当然,在CSS中你应该有类似

的东西
span.yellow-bold {
  background-color: yellow;
  font-weight: bold;
}