Solved

Spell Checker Help Part 2

Posted on 2007-03-27
3
203 Views
Last Modified: 2011-10-03
im not sure how to combine all my suggested word lists into 1 array so i can print them out.

also is their a better way to alert the user of their typos??

http://www.seandelaney.co.uk/spellchecker.php

<?php
$lang = pspell_new('en');

$misspelled = $suggested = $words = array();
$offset = 0;
$pre = '<strong>';
$post = '</strong>';
   
$string = 'Project Engineeer Job for experienced Candidate based in Aberdeeen';

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 $word)
{
      if($word <> '')
      {
            $offset = strpos($string,$word,$offset);
            $string = substr_replace($string, $post, $offset+strlen($word), 0);
            $string = substr_replace($string, $pre, $offset, 0);
            $offset = $offset+strlen($word) + strlen("$pre $post");  
      }
}

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

echo '<pre>';
print_r($suggested);
echo '</pre>';

echo '<p>Misspelled words are highlighted bold: &quot;'.$string.'&quot;</p>';
echo '<p>Suggested words are:</p>';
echo '<ul>';

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

echo '</ul>';
?>
0
Comment
Question by:ellandrd
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
elfe69 earned 500 total points
ID: 18800066
You could try something like this for example:

<?php
$lang = pspell_new('en');

$misspelled = $suggested = $words = array();
$offset = 0;
$pre = '<strong>';
$post = '</strong>';
   
$string = 'Project Engineeer Job for experienced Candidate based in Aberdeeen';

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 $word)
{
      if($word <> '')
      {
            $offset = strpos($string,$word,$offset);
            $string = substr_replace($string, $post, $offset+strlen($word), 0);
            $string = substr_replace($string, $pre, $offset, 0);
            $offset = $offset+strlen($word) + strlen("$pre $post");  
      }
}

echo '<pre>';
print_r($suggested);
echo '</pre>';

echo '<p>Misspelled words are highlighted bold: &quot;'.$string.'&quot;</p>';
echo '<p>Suggested words are:</p>';
echo '<ul>';
 
foreach($misspelled as $value)
{
      echo '<li><b>'.$value.'</b>:</li><ul>';
      $suggested[] = pspell_suggest($lang,$value);
      foreach($suggested as $suggestion)
      {      
            echo '<li>'.$suggestion.'</li>';
      }
      echo '</ul>';
}

echo '</ul>';
 
?>
0
 
LVL 16

Author Comment

by:ellandrd
ID: 18800112
thanks again
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18800204
You're welcome :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 count occurrences of each item in an array.

816 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

8 Experts available now in Live!

Get 1:1 Help Now