Solved

Spell Checker Help

Posted on 2007-03-27
8
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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