Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Spell Checker Help Part 2

Posted on 2007-03-27
3
Medium Priority
?
244 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses

971 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