2017-05-23 163 views
3

我有一个自动更正字符串的功能。它按预期更正拼写错误的单词。我面临的这个问题是,它不会将美国的拼写字改正为英国的等值。将美式英文单词转换为英式单词

$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.'); 

以上示例未检测到拼写错误。我怎样才能得到它colorcolour和相同的单词,如favoritefavourite

+0

在[Documentation page](http://php.net/manual/en/function.pspell-new.php)上有关于使用英国英国英格兰英格兰或英格兰英格兰的有趣评论。这可能值得尝试。 – Tom

+1

@thebluefox非常感谢。这帮助我解决了这个问题。如果你把这个作为答案,我会将其标记为帮助他人是正确的。 – steve

+0

完成@Steve - 很高兴我能帮到你。 – Tom

回答

1

查看pspell_new()方法的官方文档 - 有关“拼写”参数的不同值的说明 - 用于设置使用哪种版本的语言;

我认为语言和拼写参数在不同的PHP版本和/或aspell/UNIX发行版上有所不同。

我的PHP 5.2.6 Debian忽略拼写参数。

相反:

对于美国人用EN_US的语言。 英国使用EN_GB(不en_UK) 对于加拿大使用en_CA

看起来好像这个值可以根据您的服务器配置更改。