User Contributed Notes 14 notes

preg_replace_callback() is the best way I found to check words in a large text and change the HTML color of words that are not found. Here is a script I wrote which does just that, and optionally uses pspell_suggest() to auto-correct. I would love to write a better regex to ignore <text inside xml tags>, but that really isn't as easy as it sounds...<?php$pspell = pspell_new('en','canadian','','utf-8',PSPELL_FAST);

function spellCheckWord($word) { global $pspell;$autocorrect = TRUE;

// Take the string match from preg_replace_callback's array$word = $word[0];

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.

$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.

There appears to be a problem currently affecting PSpell functionality on Windows machines.

Currently, the compiled build of PHP5 for Windows does not by default include PSpell, and to activate these functions the Windows user has to (a) install GNU ASpell; (b) copy the file aspell-15.dll to a directory in the Windows "Path" statement (typically C:\Windows\System32); (c) install one or more ASpell dictionaries; (d) enable the PSpell extension to PHP by adding a reference to php_pspell.dll in the php.ini file, and moving the file itself to the main PHP directory.

However, when this has all been done and PSpell appears to have been fully activated, there remains a problem. While all the PSpell functions work as specified when referenced from the "main" body of PHP code, if they are placed within a PHP function, the system reports that it is unable to find any word lists for the specified dictionary (whichever one is chosen).

I have a sample of PSpell-using code (taken from the SAMS book PHP 5 In Practice) which works perfectly on a Unix-based Web Server, but which fails with the indicated error message on a range of Windows machines.

Since Aspell 0.50, Pspell is no longer used. It is not necessary to download and install the Pspell package, only Aspell 0.50 (or better) is required. Aspell now provided a backwards compatibility interface for programs expecting the old Pspell interface (such as PHP).

Even though Aspell now provided a new "Aspell" interface this, the PHP Pspell interface is the one you want to use. The Aspell PHP interface is the one used with a *very* old version of Aspell as the manual says.

Sorry if this is confusing, but it is not really my fought as I have no control over PHP development. Eventually a new Aspell interface should be provided for PHP which uses the new Aspell interface provided with Aspell 0.50 but for now the Pspell interface will work just fine.

Note: If you wish to use an older version of Aspell (_before_ 0.50) than both Aspell and Pspell are required.

As an alternative to mucking about with the compiled-in aspell/pspell functions, which only check word by word, one could write a script that calls aspell as a shell exec to check a whole block of text: