Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

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: &quot;'.$string.'&quot;</p>';

$wordlist = preg_split('/[\W]+?/',$string);

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,$value))
      {
            $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>';
?>
Avatar of elfe69
elfe69
Flag of Switzerland image

Try removing the [1] in your last foreach loop:

foreach($suggested as $suggestion)
{
      echo '<li>'.$suggestion.'</li>';
}
Avatar of ellandrd

ASKER

i get blank list

Avatar of Vel Eous
Vel Eous

Have you declared the variable $word outside of your first for loop ?
no
Try putting some print_r() statements between the loop in order to check that your arrays are not empty
ive uploaded new version so you can see...
ASKER CERTIFIED SOLUTION
Avatar of elfe69
elfe69
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you