Solved

Spell Checker Help Part 2

Posted on 2007-03-27
3
193 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

9 Experts available now in Live!

Get 1:1 Help Now