PHP spell checking script

This is a spelling checking script written in PHP/MySQL. It asks the user to input an English word and checks if the spelling is correct. If the spelling is not correct, it offers different suggestions of similar words.
The script makes use of a list of words present in the English dictionary to check the spellings. The list can be found here. It is a text file, you can import it into a database using phpmyadmin or any other similar tool. Recommended way of importing the words is by executing the following query.

Once the database is filled up with records, we connect our script to the database using mysql_connect() and mysql_select_db() functions.


After connecting to the database, we include this file using php require();.

Now, our next step is to create the main form and processing the word entered by the user.
We write a function spell_check() to process the word entered by the user.
Firstly, we trim the input string provided by the user to ensure that no empty string is provided. Next we find the first alphabet of the word by using php’s substring() function.
After getting the first alphabet of the word, we pass it on the sql query as a search criteria. So basically, only those words will be selected whose first alphabet is same as the word’s first alphabet.

Now we run a while loop to traverse to all the result sets. We also check if the word is present in the set by using the in_array() function. If it is not then it means that the user may have probably entered the wrong spelling. So our task in the while loop is to match the words in the result set with the user provided word using the similar_text() method. If the matching percent is more then 85% then we consider suggesting the word to the user. If not, we just let it go. So that was a brief idea on the working of the script.


The following two tabs change content below.
  • Ranjan


    How to insert the english dictionary text file into the database..

  • Vlad

    You can use LOAD DATA INFILE as i have used in the above post.
    Again, syntax: LOAD DATA INFILE ‘path_to_english.txt’ INTO ‘tablename’

  • Ritu

    How to use spell check code for sentance of words, means for multiple words at a time.

  • Brandon

    how would you account for a user missing only the first letter? also, in_array is case sensitive when checking strings.