我想快速检查是否有UTF-8单词作为数组键。
这些单词可能具有:
我可以使用mb_strtolower()
使它们都小写,并使用Normalizer::normalize()
标准化字符串。这会检查前2个项目符号要点,但不处理重音符号:
'tést' !== 'test'
我可以使用Collator
比较两个词:
$collator = new Collator('fr_FR');
$collator->setStrength(Collator::PRIMARY);
$collator->compare('tést', 'test'); // 0
这检查了我的3个要点,但是当我希望能够执行二进制查找作为数组键时,我必须遍历所有单词对以进行比较(我有很多查找要在数组上执行字典)。
我想要的是:
function reduce($word) {
// how?
}
// prepare the dictionary (once)
$dictionary = [];
foreach ($dictionaryWords as $dictionaryWord) {
$dictionary[reduce($dictionaryWord)] = true;
}
// perform a lookup (many times)
if (isset($dictionary[reduce($lookupWord)])) {
// it's a match!
}
基本上,我希望reduce()
函数(可能命名不正确)执行这样的简化:
我相信MySQL在内部为其文本索引执行类似的操作。
是否有intl
功能可以做到这一点? list of intl
classes and functions难以消化。
intl
或者如果您担心的话,请使用对碰撞攻击更具弹性的算法。如果您需要该区域的任何指针,请详细说明您的问题。