Back in 10/2002 csnyder at chxo dot com wrote the first comment about the spell functions, and wrote a very sophisticated spell check with a direct call to aspell. In a similar vein, I wrote a simplified a PHP function that will spell check a string and insert tags before and after the misspelled words. Auto-correction is not offered.
<?
function spellcheck($string) {
// Entry: $string: Text to spellcheck.
// Returns: string with '<strong>' and '</strong>' inserted before and
// after misspellings.
$pre='<strong>'; // Inserted before each mis-spelling.
$post='</strong>'; // Inserted after each mis-spelling.
$string=strtr($string,"\n"," ");
// Drop newlines in string. (It bothers aspell in this context.)
$mistakes = `echo $string | /usr/local/bin/aspell list`;
// Get list or errors.
$offset=0;
foreach (explode("\n",$mistakes) as $word)
// Walk list, inserting $pre and $post strings. I move along and
// do the insertions, keeping track of the location. A global replace
// with str_replace($string,$pre.$work.$post) is problematic if the
// same misspelling is found more than once.
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");
};
return $string;};
?>
For this to work on your system, see if /usr/local/bin/aspell list runs from the shell. It needs to get input from standard input. You may not be able to run apell without the path for PHP because the PATH variable may be different in the PHP invocation from a shell invocation.