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.
|
1 |
LOAD DATA INFILE 'path_to_english.txt' INTO 'tablename' |
Once the database is filled up with records, we connect our script to the database using mysql_connect() and mysql_select_db() functions.
config.php:
|
1 2 3 4 |
<?php mysql_connect('localhost','mycoding','123') or die(mysql_error()); mysql_select_db('english') or die(mysql_error()); ?> |
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.
|
1 2 3 4 |
<?php $first_word = substr($word,0,1); $query = "SELECT * FROM words where LEFT(word,1) = '$first_word'"; ?> |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<title>Spell Checker</title> <?php function spell_check($word) { require 'config.php'; $first_word = substr($word,0,1); $query = "SELECT * FROM words where LEFT(word,1) = '$first_word'"; $db_words = mysql_query($query); $print = 0; $flag = true; while(($row_words = mysql_fetch_array($db_words)) && !in_array($word,$row_words)){ similar_text($row_words['word'],$word,$percent); if($percent>85) { if($print == 0)echo '<strong>Did you mean:</strong><br> '; $print++; echo $row_words['word']."<br>"; $flag = false; } } if($flag){ echo "Correct spelling!<br>"; } } ?> <?php if (isset($_GET['word']) && trim($_GET['word']) != null) { $word = trim($_GET['word']); spell_check($word); } ?> <form action = "" method ="get"> <input type = "text" name = "word"> <input type = "submit" name = "check" value = "Check Spelling"> </form> |
Output:

Hi,
How to insert the english dictionary text file into the database..
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’
How to use spell check code for sentance of words, means for multiple words at a time.
how would you account for a user missing only the first letter? also, in_array is case sensitive when checking strings.