Solved

Spell Checker Help

Posted on 2007-03-27
8
303 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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