...

View Full Version : Spell Checker



DanielBr
02-01-2010, 07:38 PM
Hi all I am just posting to see if someone could offer advice on where this script is going wrong. As the title implies it is supposed to be a basic spell checker, which informs the user what words, if any, they have gotten wrong. It is simple enough to follow but if anyone wants me to edit it with comments let me know, and I will be happy to oblige :)




<?php

$usersEntry = $_POST[text];
$words = explode(" ", $usersEntry);
$dictionary = fopen("dictionary/dictalt.txt", "r");
$words=preg_replace("/[^a-zA-Zs]/", "", $words);
$content = fgetc($dictionary);

$wrong=0;
$print="";

foreach($words as $word)
{

while (!feof($dictionary))
{

if ($word <= $content)
{
$content = fgetc($dictionary);
}
else
{
fclose($dictionary);
break;
}

}
if ($word == $content)
{
$print=$print.' '.$word.' ';
echo $word." is correct";
}
else
{
$print=$print.' <span class="wrong">'.$word.'</span> ';
$wrong++;
}

}


echo'<h3>You made '.$wrong.' mistake(s) which are highlighted below</h3>
<p>'.$print.'</p>';



I think it is a string comparison issue, I am aware of the strcmp function but even using that I could not get a positive comparison. The main problem is that it states every word is incorrect no matter what the spelling !

I would appreciate any input/advice it is driving me crazy now :p

mlseim
02-01-2010, 08:07 PM
Doing spell checking is a tough thing.

Not sure what your final application is, but there is a free script
that uses "pspell", which is normally enabled for PHP5 webhost users.

Check this out and see if it might be a better way to do it.
http://www.broken-notebook.com/spell_checker/index.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum