這裏是一個PHP類,將通過利用PHP類Transliterator
,這是因爲PHP 5.4,並安裝了intl伸展的突出一些HTML文本的搜索詞的所有事件。
該類將音譯HTML中的每個字符,然後對搜索項和音譯HTML進行字符明細比較。它將使用提供的$css_class
使用HTML span
元素突出顯示匹配項。
該類還支持其音譯產生多於一個字符的字符,例如,日文字符手
音譯爲shou
,因此如果搜索字詞爲should
,則字符手ld
將在文本中突出顯示。
該類僅受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;
}
你的意思是「weshá」不被大膽替換,當它出現在$行[「定義」]?我是否正確? –
不,沒有被取代,你得到它的權利.... –
對我來說它的工作,我的意思是不替換,但如果我搜索'wesha'或weshá'這個詞(兩者)大膽 –