I recently undertook the task of implementing a spell checking solution using PHP. The solution was intended to be used on a website that allows users to submit content.
I had a few specific goals for the spell checker in mind before I started. The solution needed to be able to allow users to spell check HTML form submitted content by showing users potentially misspelled words, allowing users to click on "suggested" replacement words and allowing users to add words to a custom dictionary. The solution also needed to be PHP based and NOT use client side stuff such as JavaScript (it has to work always, not just on particular browsers and not just when certain features of a browser are enabled).
I was all set for a major endeavor and was pleasantly surprised that with PHP this is relatively easy.
First off you have to a spell checking library. The aspell/pspell package available for just about any UNIX platform fits this bill. Secondly, PHP has to have aspell/pspell support compiled in. Lastly, you gotta write some PHP to use the aspell/pspell stuff via the pspell PHP functions.
Here is the more specific breakdown:
I use PHP functions and include files. Thats just my style. I prefer the functions with PHP to OO PHP and classes. I understand OO and actually use Java more often than PHP, but to me the PHP OO is quite verbose, so I just use functions and it saves a lot of typing ->.
I created a "spell.inc" file to hold my spelling functions. Specifically it needed to check a word, display the list of potential replacement words, and be capable of allowing a user to add a word.
I then created a "spellCheck.php" file that used the functions in spell.inc to parse form fields (I store the form field value in the session, so I can manipulate it, such as when a user selects one of the suggested words). The spellCheck.php file contains links (HTTP_GET) that are processed by a "spellCheck_process.php" page (they are links to that page with GET variables passed). spellCheck_process.php either adds words to the custom dictionary or replaces potentially misspelled words with suggested replacements depending on which link on spellCheck.php the user clicks.
When the user is done they simply click a "back to the form" button that takes them back to the form with the updated values (again, session) and they can then submit the form.
Well, thats a lot of hype, here is the code: phpSpellCheck.html
(I would have included it all here in the article, but the code examples did not format well in this space).
This is not meant to be an end all means, rather an example of one implementation, but feel free to use and critique it. If you have any questions or issues just let me know via the phorum link below (the "comments" to this article).
Go forth and spell correctly!
phpSpellCheck.html (CODE)
Comments
Re: Creating a Spell Checker with PHP
I am interested in making this work for me, and I am having problems. I am newbie, but have been able to do some things with php. I would like to see this work. where is a url to a form that this is working on?
thank you
Lee
You can even do it .NET
Hey
Ya i agree with creating a spell check isnt really that hard once you get hold of a library which does, today there like libraries for every format php, .net, java, javascript.
An article on vb here shows it just abou 100 lines of code do that
http://www.mabaloo.com/Software-Development/Spell-Checker-Routine-Using-VB.html
pretty simple I guess :)