Solved

Spell Checker Help

Posted on 2007-03-27
8
292 Views
Last Modified: 2007-03-27
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>';
?>
0
Comment
Question by:ellandrd
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:elfe69
ID: 18799586
Try removing the [1] in your last foreach loop:

foreach($suggested as $suggestion)
{
      echo '<li>'.$suggestion.'</li>';
}
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18799595
i get blank list

0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 18799597
Have you declared the variable $word outside of your first for loop ?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 16

Author Comment

by:ellandrd
ID: 18799611
no
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18799659
Try putting some print_r() statements between the loop in order to check that your arrays are not empty
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18799663
ive uploaded new version so you can see...
0
 
LVL 11

Accepted Solution

by:
elfe69 earned 500 total points
ID: 18799702
Ok, you store your suggestions in an array that is indexed on the word that is misspelled in the first array, if you do not need to print the misspelled word againm, changing the previous loop would give you the result:

foreach($misspelled as $value)
{
      $suggested[] = pspell_suggest($int,$value);
}


0
 
LVL 16

Author Comment

by:ellandrd
ID: 18799762
thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question