我有一个自动更正字符串的函数。它按预期纠正了拼写错误的单词。我面临的这个问题是,它不会将美式拼写单词更正为英式拼写单词。
$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
?
最佳答案
查看 pspell_new()
的官方文档方法 - 关于“拼写”参数的不同值的注释 - 用于设置使用的语言版本;
I think the language and spelling parameters differs on different PHP versions and/or aspell/UNIX distributions.
My PHP 5.2.6 Debian ignores the spelling parameter.
Instead:
For Americans use en_US as language. For British use en_GB (not en_UK) For Canadian use en_CA
看起来这个值可能会根据您的服务器配置而改变。
关于php - 将美式单词转换为英式单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44131047/