CancelImage Upload

Building a spell check for your PHP applications with the help of PSpell

Building a spell check for your PHP applications may sound like a bit of a daunting task. The question of where to start may come up, as copying word for word out of countless dictionaries would seem to be a mind numbing task at least 99.9% of all programmers would love to avoid at all cost.

Lucky for us lot then 0.1% decided to take on the challenge and create the PHP Library PSpell (Portable Spell Checker Interface Library), which makes building an online spell check facility the work of 10 minutes (or 5 if you take out that coffee break).

Before PSpell's useful functions can be used however, you will need to check that it has indeed already been made a component of your PHP Library. If not you can download it from here:

http://pspell.sourceforge.net

Once integrated you can start by opening a new handle and choosing your required language. Here in our example we will call upon English:

<?php

$pspell_link = pspell_new("en");

?>


Once we have the handle we can use the function pspell_check() to enquire if a word was spelt correctly. Note that pspell_check() can only handle one word at a time. If you want to scan through an entire document then you will require something along the lines of:

<?php

$pspell_link = pspell_new("en");

$string = "This isss my textd and it has somen errors";

$string = split(" ", $string);

for($i = 0; $i != sizeof($string); $i++)
{

      if(pspell_check($pspell_link, $string[$i]))
      {

            echo $string[$i]." ";

      }
      else
      {

            echo "<b style=\"color:#FF0000;\">".$string[$i]."</b> ";

      }

}

?>

The above code uses the function split() to separate each word contained within $string, and then scans the words one by one. Those that have been found to be with errors will be highlighted bold and red.

To take things one step further we will require a spell suggest feature. For this we will make use of, you guessed it, pspell_suggest():

<?php

$pspell_link = pspell_new("en");

$string = "This isss my textd. And it has somen errors.";

$string = split(" ", $string);

for($i = 0; $i != sizeof($string); $i++)
{

      if(!pspell_check($pspell_link, $string[$i]))
      {

            $suggestions = pspell_suggest($pspell_link, $string[$i]);

            echo "Suggestions for: ".$string[$i]."<br><br>";

            for($j = 0; $j != sizeof($suggestions); $j++)
            {

                  echo $suggestions[$j]."<br>";

            }

            echo "<br>";

      }

}

?>

And there you have it, two useful spell check functions just waiting to be used. Of course they will require some fine tuning. Especially the split() needs to be turned into a preg_split() to separate commas, exclamation marks and full stops. With a bit of work though, and some AJAX, the above code could serve as a pretty good foundation for a solid spell check facility!

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com