Solved

Spell Checker Help

Posted on 2007-03-27
8
263 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:Tchuki
ID: 18799597
Have you declared the variable $word outside of your first for loop ?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18799611
no
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now