简体   繁体   中英

Convert American words to British equivalent

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM