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
Solved

Spell Checker Help Part 2

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

856 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