Miscellaneous
  Home arrow Miscellaneous arrow Page 13 - 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 13 of 14 )

    Now, by this point we have accepted the user input, found the misspelled words, and given the user a form by which they can correct the misspelled words. Now, it is time to apply the corrections that the user made. As seen by the action of the previous form, we will call this page spell2.php. The first thing we will do is to copy the data from the $_POST array into easier to use variables. As with the last posted data, we will check to see if magic quotes are enabled and handle the data appropriately.

    <?php
    $corrections 
    = array();

    if (
    get_magic_quotes_gpc()) {
      
    $text stripslashes ($_POST['original']);
      foreach(
    $_POST['corrections'] as $key=&gt;$value) {
        
    $corrections[$key] = stripslashes($value);
      }
    } else {
      
    $text $_POST['original'];
      
    $corrections $_POST['corrections'];
    }
    ?>

    We will split the original text with the same regular expression used in the beginning.

    <?php
    $words 
    preg_split ("/[\s,.]+/"$text);
    ?>

    Now, we will loop through each correction that needs to be made and apply it. As we apply the corrections, we will surround the corrected word with HTML comments. This will enable us to ensure that we don't replace already replaced words.

    <?php
    foreach($corrections as $key=&gt;$correction) {
        
    $pattern "/(\A|[^(\&lt;!\-\-replaced\-\-\&gt;)A-z])"
                 
    "{$words[$key]}(\Z|[^(\&lt;!\-\-replaced\-\-\&gt;)A-z])/";
        
    $text preg_replace($pattern
                     
    "\\1&lt;!--replaced--&gt;$correction&lt;!--replaced--&gt;\\2"
                     
    $text1);
    }
    ?>

    At this point, all the corrections have been made and all this is left to do is display our result.

    <?php
    echo nl2br($text);
    ?>

    An application such as this spell checker can be used in many different situations. If you are added news to your site, wouldn't it be great to spell check it before publishing? Users would be sure to appreciate spell checking of forum posts before they go live. You could incorporate this into the preview option on your forums. Just about anywhere, you accept text from the user, spell checking can be used.

    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