ellandrd
asked on
Spell Checker Help
im trying to get a small script working that checks each word in a string and if its misspelled show suggestions, but my suggestions wont show. i get this instead:
Warning: Invalid argument supplied for foreach() in spellchecker.php on line 50
live version:
http://www.seandelaney.co.uk/spellchecker.php
my code:
<?php
$misspelled = $suggested = $words = array();
$lang = pspell_new('en');
$string = 'Project Engineeer';
echo '<p>Your Phrases was: "'.$string.'"</p >';
$wordlist = preg_split('/[\W]+?/',$str ing);
for($i = 0; $i < count($wordlist); $i++)
{
$word = trim($wordlist[$i]);
if(!preg_match('/[A-Za-z]/ ',$word))
{
continue;
}
$word = trim($word);
if(!in_array($word,$words, true))
{
$words[] = $word;
}
}
foreach($words as $value)
{
if(!pspell_check($lang,$va lue))
{
$misspelled[] = $value;
}
}
foreach($misspelled as $value)
{
$suggested[$value] = pspell_suggest($int,$value );
}
echo '<p>Suggested words are:</p>';
echo '<ul>';
foreach($suggested[1] as $suggestion)
{
echo '<li>'.$suggestion.'</li>' ;
}
echo '</ul>';
?>
Warning: Invalid argument supplied for foreach() in spellchecker.php on line 50
live version:
http://www.seandelaney.co.uk/spellchecker.php
my code:
<?php
$misspelled = $suggested = $words = array();
$lang = pspell_new('en');
$string = 'Project Engineeer';
echo '<p>Your Phrases was: "'.$string.'"</p
$wordlist = preg_split('/[\W]+?/',$str
for($i = 0; $i < count($wordlist); $i++)
{
$word = trim($wordlist[$i]);
if(!preg_match('/[A-Za-z]/
{
continue;
}
$word = trim($word);
if(!in_array($word,$words,
{
$words[] = $word;
}
}
foreach($words as $value)
{
if(!pspell_check($lang,$va
{
$misspelled[] = $value;
}
}
foreach($misspelled as $value)
{
$suggested[$value] = pspell_suggest($int,$value
}
echo '<p>Suggested words are:</p>';
echo '<ul>';
foreach($suggested[1] as $suggestion)
{
echo '<li>'.$suggestion.'</li>'
}
echo '</ul>';
?>
ASKER
i get blank list
Have you declared the variable $word outside of your first for loop ?
ASKER
no
Try putting some print_r() statements between the loop in order to check that your arrays are not empty
ASKER
ive uploaded new version so you can see...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you
foreach($suggested as $suggestion)
{
echo '<li>'.$suggestion.'</li>'
}