Convert American words to British equivalent

208 views Asked by At

I have a function that auto corrects a string. It corrects misspelled words as expected. This issue I'm facing is that it will not correct an American spelled word into it's British equivalent.

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

The above example does not detect a spelling error. How can I get it to change color to colour and the same for words such as favorite to favourite?

1

There are 1 answers

0
Tom On BEST ANSWER

Looking at the official documentation for the pspell_new() method - there's a comment regarding differing values for the "spelling" parameter - which is used to set which version of a language is used;

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

It looks as if this value could change depending on your server configuration.