3

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?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
steve
  • 471
  • 6
  • 15
  • There's an interesting comment on the [Documentation page](http://php.net/manual/en/function.pspell-new.php) regarding the use of `british`, `en_GB` or `en_UK`. It might be worth experimenting with those. – Tom May 23 '17 at 09:47
  • 1
    @thebluefox thanks a lot. this helped me fix the issue. If you put this as an answer I will mark it as correct to help others. – steve May 23 '17 at 10:36
  • Done @Steve - glad I could help. – Tom May 23 '17 at 12:28

1 Answers1

1

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.

Community
  • 1
  • 1
Tom
  • 4,257
  • 6
  • 33
  • 49