# 添加关键词 到全局字典dict里面
protected function addWord($strWord)
{
$len = mb_strlen($strWord,'UTF-8');
$curNode = &$this->dict;
for ($index = 0; $index < $len; $index++) {
$word = mb_substr($strWord, $index, 1, 'UTF-8');
if (!isset($curNode[$word])) {
$curNode[$word] = [];
}
$curNode =& $curNode[$word];
}
$curNode['end'] = 1;
}
# 根据字典dict搜索关键词
public function searchMatchWords($strWords)
{
$len = mb_strlen($strWords, 'UTF-8');
$result = [];
for ($index = 0; $index < $len; $index++) {
$wordLength = $this->searchWord($strWords, $index);
if ($wordLength > 0) {
$words = mb_substr($strWords, $index, $wordLength, 'UTF-8');
if (!isset($result[$words])) {
$result[$words] = 1;
} else {
$result[$words] = $result[$words]++;
}
$index += $wordLength - 1;
}
}
return $result;
}
protected function searchWord($strWords, $beginIndex)
{
$len = mb_strlen($strWords, 'UTF-8');
$curNode = $this->dict;
$isEnd = false;
$wordLength = 0;
for ($index = $beginIndex; $index < $len; $index++) {
$word = mb_substr($strWords, $index, 1, 'UTF-8');
if (!isset($curNode[$word])) {
break;
}
$wordLength++;
$curNode = $curNode[$word];
if (isset($curNode['end'])) {
$isEnd = true;
}
}
if (!$isEnd) {
$wordLength = 0;
}
return $wordLength;
}
参考文档