Miscellaneous
  Home arrow Miscellaneous arrow Page 12 - Form and Spelling Validation
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Form and Spelling Validation
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-07-20

    Table of Contents:
  • Form and Spelling Validation
  • Common Form Validations
  • Spell Checking
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 submitted
  • Strip backslashes if necessary
  • Set up our variables
  • Create the spell checking session
  • Check each word in the text for proper spelling
  • Recreate 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=&gt;$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=&gt;$word) {
      
    $dropdown '&lt;select name="corrections[' $key ']"&gt;';
      
    $dropdown .= '&lt;option value="' 
            
    htmlentities($words[$key]) . '"&gt;' 
            
    htmlentities($words[$key]) . '&lt;/option&gt;';
      foreach(
    $suggestions["$word"] as $suggestion) {
        
    $dropdown .= '&lt;option value="' 
              
    htmlentities($suggestion) . '"&gt;' 
              
    htmlentities($suggestion) . '&lt;/option&gt;';
      }
      
    $dropdown .= "&lt;/select&gt;";

      
    $pattern "/(\A|[^(E=\")&gt;A-z]){$words[$key]}(\Z|[^\"(&lt;\/O)A-z])/";
      
    $text preg_replace($pattern"\\1$dropdown\\2"$text1);
    }
    ?>

    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!&lt;br /&gt;&lt;br /&gt;\n";
    } else {
      echo "&lt;form method=\"POST\" action=\"spell2.php\"&gt;\n";
      echo nl2br($text);
      echo "&lt;input type=\"hidden\" name=\"original\" value=\"" 
         . htmlentities($original) . "\"&gt;\n";
      echo "&lt;br /&gt;&lt;br /&gt;&lt;input type=\"submit\" name=\"submit\" "
         . "value=\"Correct\"&gt;\n";
      echo "&lt;/form&gt;\n";
    }

    More Miscellaneous Articles
    More By Matt Wade


       · 
       · Comments given fo title
       · sorry I ran this function and itdidn't work.
       · Remove the echo statement.
       · td.error {color:C03; font-weight:bold; }is incorrect, should be:td.error...
       · td.error {color: red; font-weight:bold; }
       · :)
       · I dont know how this is supposed to work but it doesnt..I can get the spell...
       · HOLY CRAP! I have been stuck on this for about a week and have spent about 6-10...
     

  • MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT