Implementing Spell Check in PHP using Pspell

If you want to implement spellcheck functionality in PHP then believe me, there is no easy way to do it. PHP has some built in functions to check the similarity between two strings. Two of these are levenshtein and similar_text. But you still need a database of words to match the input. PHP has an extension Pspell to do it, but due to lack of documentation, it is little bit difficult to implement. Here we will see how to implement the spell check functionality in PHP with Pspell.

Installing Pspell on Linux:

Here is the linux command to install the Pspell extension:

yum install aspell

sudo apt-get install php5-pspell

Installing Pspell on Windows:

On a Windows system, download and install GNU aspell together with at least one dictionary and then use the search facility in Explorer to find the aspell.exe file. The path should look like:

c:program filesaspellbinaspell.exe

Installing dictionaries in Pspell:

yum install aspell-en.i586

More pspell dictionaries are available here.

Checking spellings with Pspell and PHP:

    function suggest($string)
        $suggestions = array();

        // Suggests possible words in case of misspelling
        $config_dic = pspell_config_create('en');

        // Ignore words under 2 characters
        pspell_config_ignore($config_dic, 2);

        // Configure the dictionary
        pspell_config_mode($config_dic, PSPELL_FAST);
        $dictionary = pspell_new_config($config_dic);

        if (!pspell_check($dictionary, $string)) {
            $suggestions = pspell_suggest($dictionary, $string);

        return $suggestions;

$suggestions = suggest("wordl");

Installing Custom Dictionary in Pspell:

● Create a file called my_dict.txt in your home directory. This file contains one word per line (separated by UNIX EOLs): touch my_dict.txt
● Install the text file in aspell: sudo aspell –lang=en create master /usr/lib/aspell/my_dict.rws < my_dict.txt. In some linux installations the path may be /usr/lib64/aspell/my_dict.rws
● Now go to the aspell directory: cd /usr/lib/aspell/
● Then edit: sudo vi en.multi and add this line: add my_dict.rws

Now the newly added words will be available in your spell checker application.

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.


3 thoughts on “Implementing Spell Check in PHP using Pspell

Comments are closed.