Solved

Spell Checker Help Part 2

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 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

14 Experts available now in Live!

Get 1:1 Help Now