Form and Spelling Validation -
(Page 12 of 14 )
Now that we have the data from the user, it is time to actually process the text and check it for proper spelling. We take care of these tasks in a file names spell.php. Let's take an overview of the processes we will go through, then we will look at the code.
Verify the submittedStrip backslashes if necessarySet up our variablesCreate the spell checking sessionCheck each word in the text for proper spellingRecreate text with drop downs for suggestions
The first thing we need to do is to make sure that some data was actually submitted to the script. We will accomplish this by simply checking for the existence of the form variable. If it exists we allow the script to continue, otherwise we stop the script execution.
<?php if(!isset($_POST['text'])) { exit('No text submitted.'); } ?> |
Next, we need to remove any backslashes from the text if they were automatically added. To determine this, we will check to see if magic quotes are on. If they are, we will strip the slashes. At the same time, we will also place the text into a more manageable variable name.
<?php if (get_magic_quotes_gpc()) { $text = stripslashes ($_POST['text']); } else { $text = $_POST['text']; } ?> |
The next step is to set up some arrays that we will use to track misspelled words and suggestions. We will also make a copy of the original text and split the text into individual words and store them in an array. To do this, we will use the preg_split() function and split words on a white space character, comma, or period.
<?php $misspelled = array(); $suggestions = array(); $original = $text; $words = preg_split ("/[\s,.]+/", $text) ?> |
Now, as with our simple example, we set up our spell check session. Again, you can specify a different language here if you wish.
<?php $pl = pspell_new ("en"); ?> |
We are now ready to actually spell check the words. We will check each word separately and find suggestions for any misspelled words. Before checking the word for spelling, we remove any characters that are not letters of the alphabet, a hyphen, or an apostrophe. This will remove any characters such as parentheses from the words and eliminate false negatives.
If a word is found to be misspelled, we store it in the $misspelled array. Not only do we store it in the $misspelled array, we store it using the same key as it had in the $words array. The reason for this is that we need some way to identify later where a replacement will go in the text. We cannot rely on blindly substituting the replace for the misspelled word as this could cause problems in cases where there are two or more instances of a misspelled word. If the end user wants to replace each occurrence of the misspelled word with a different replacement, the blanket substitution method would not work. Hence the reason we utilize the key, which specifies the word location, when storing the misspelled word.
Also at the point of finding a misspelled word, we will store the suggestions in an array called $suggestions. In order to conserve a bit of memory, we use the misspelled word as the key for this array. In cases where a user consistently misspells the same word in a body of text, using the misspelled word as the key will eliminate storing the same suggestions repeatedly.
<?php foreach($words as $key=>$word) { $checkword = preg_replace("/[^A-z-']/", '', $word); if (!pspell_check ($pl, $checkword)) { $misspelled[$key] = $checkword; $suggestions[$checkword] = pspell_suggest ($pl, $checkword); } } ?> |
Now that we have gone through each word and checked it for proper spelling, we are ready to build a form that contains the original text with suggestions incorporated. To build the form, we will need to create a select list with all the suggestions for each misspelled word. Therefore, we will examine each of the elements in the $misspelled array and store the proper information to create the select list in a variable called $dropdown. To enable us to properly identify which suggestion was chosen for a given misspelled word, we will use the key from the $misspelled array as the index of the array for our select lists.
Once we build the select list, we then replace the misspelled word with it. To do this, we use a regular expression that replaces the misspelled word as long as it is not part of a select list already. We must make sure that it is not part of a select list, otherwise when we have multiple instances of a misspelled word we would end up with incorrect results caused by nesting. The regular expression also specified that there should not be an alphabetic letter on either side of the match. This ensures that we don't put the drop down list inside a word that might contain the same characters as the misspelling. We also limit the regular expression to replace only the first match. This limiting is needed in cases where there are multiple instances of a misspelled word.
<?php foreach($misspelled as $key=>$word) { $dropdown = '<select name="corrections[' . $key . ']">'; $dropdown .= '<option value="' . htmlentities($words[$key]) . '">' . htmlentities($words[$key]) . '</option>'; foreach($suggestions["$word"] as $suggestion) { $dropdown .= '<option value="' . htmlentities($suggestion) . '">' . htmlentities($suggestion) . '</option>'; } $dropdown .= "</select>";
$pattern = "/(\A|[^(E=\")>A-z]){$words[$key]}(\Z|[^\"(<\/O)A-z])/"; $text = preg_replace($pattern, "\\1$dropdown\\2", $text, 1); } ?> |
At this point, we have checked each word for spelling and created a select list for each misspelling that contains a list of suggestions. Now, we need to display the text back to the user so that they can make their changes. To facilitate displaying the text, we will output the opening form tag, followed by the modified text, and then a submit button with the closing form tag. We also need to pass the original text to the next page so that we have it to work with. We will accomplish this by placing the text in a hidden form field.
If there were no misspellings in the text, we will inform the user of such.
if(count($misspelled) == 0) { echo "NO misspelled words!<br /><br />\n"; } else { echo "<form method=\"POST\" action=\"spell2.php\">\n"; echo nl2br($text); echo "<input type=\"hidden\" name=\"original\" value=\"" . htmlentities($original) . "\">\n"; echo "<br /><br /><input type=\"submit\" name=\"submit\" " . "value=\"Correct\">\n"; echo "</form>\n"; } |
Next: >>
More Miscellaneous Articles
More By Matt Wade