Miscellaneous
  Home arrow Miscellaneous arrow Page 10 - 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 
Dedicated Servers  
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 10 of 14 )

    Before we build our spell checker, let's look at the basic functionality of the pspell() functions and see how we can check individual words for spelling errors.

  • pspell_new ( string language ) - This function starts a spell check session. It returns a link identifier that is used by the other pspell() functions.
  • pspell_check ( int dictionary_link, string word) - When given a word, this function will return TRUE if it is spelled correctly or FALSE if it is not.
  • pspell_suggest ( int dictionary_link, string word) - This function will return an array populated with potential spellings of any given word.

    There are a few other functions supplied by the pspell() extension, but for our purposes we won't need them. These other functions provide methods for personal wordlists and a couple of configuration options.

    First off, let's look at a very basic example of how to spell check a word. We will first start a spell check session. Then we will pass a word to the pspell_check() function and examine the result we receive to determine if the word was spelled properly or not.

    <?php
    $word 
    'recieve';
    $pspell_resource pspell_new('en');
    if(
    pspell_check($pspell_resource$word)) {
      echo 
    "'$word' is spelled correctly.&lt;br /&gt;\n";
    } else {
      echo 
    "'$word' is not spelled correctly.&lt;br /&gt;\n";
    }
    ?>

    You will notice that we passed the string 'en' to the pspell_new() function. This identifies the language to use for spell checking. Many different languages are supported. Which dictionaries are available for your use depends on what was installed with the aspell() libraries. To obtain a list of installed dictionaries, issue the following command from a shell prompt on your server.

    &gt; aspell dump dicts

    You can also execute the following PHP script to obtain the list of dictionaries.

    &lt;?php
    echo nl2br(`aspell dump dicts`);
    ?&gt;

    Going to back the example code, the next thing we do is use the pspell_check() function to determine if the word stored in the $word variable is spelled correctly or not. Depending on the outcome, we display the appropriate statement.

    Now, we will add in the pspell_suggest() function. This function will take a word and find other ways it could possibly be spelled. One thing to note is that this function makes no distinction between a properly spelled word and an incorrectly spelled one. It will suggest alternative spelling for any word given to it. Let's add in the suggestion functionality in the else clause.

    <?php
    } else {
      echo 
    "'$word' is not spelled correctly.&lt;br /&gt;\n";
      echo 
    "Possible correct spellings:&lt;br /&gt;\n";
      
    $suggestions pspell_suggest($pspell_resource$word);
      foreach(
    $suggestions as $suggest) {
        echo 
    "$suggest&lt;br /&gt;\n";
      }
    }
    ?>

    With that addition, we now have a list of suggestions for the misspelled word. Now that we have the basics down, let's move on to our spell checking application.

    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 4 hosted by Hostway