我有一个自动校正字符串的函数。它按照预期纠正拼写错误的单词。我面临的问题是,它不会将一个美国拼写的单词更正为英国的同义词。
$pspell = pspell_new('en','british','','utf-8',PSPELL_FAST);
function spellCheckWord($word) {
global $pspell;
$autocorrect = TRUE;
// Take the string match from preg_replace_callback's array
$word = $word[0];
// Ignore ALL CAPS
if (preg_match('/^[A-Z]*$/',$word)) return $word;
// Return dictionary words
if (pspell_check($pspell,$word))
return $word;
// Auto-correct with the first suggestion
if ($autocorrect && $suggestions = pspell_suggest($pspell,$word))
return current($suggestions);
// No suggestions
return $word;
}
function spellCheck($string) {
return preg_replace_callback('/\b\w+\b/','spellCheckWord',$string);
}
echo spellCheck('This is a color.'); 上面的示例没有检测到拼写错误。如何将color更改为colour,将favorite改为favourite等单词相同?
发布于 2017-05-23 10:39:37
查看pspell_new()方法的正式文档--对于“拼写”参数的不同值有一个注释,该参数用于设置使用哪种语言版本;
我认为不同的PHP版本和/或A拼写/UNIX发行版的语言和拼写参数不同。 我的PHP5.2.6 Debian忽略了拼写参数。 相反: 对于美国人来说,使用en_US作为语言。英国使用en_GB (非en_UK)加拿大使用en_CA
看起来这个值可能会根据您的服务器配置而改变。
https://stackoverflow.com/questions/44131047
复制相似问题