PHP Doku:: Pspell Funktionen - ref.pspell.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzUnterstützung menschlicher Sprache und ZeichenkodierungPspellPspell Funktionen

Ein Service von Reinhard Neidl - Webprogrammierung.


<<Vordefinierte Konstanten



Pspell Funktionen


16 BenutzerBeiträge:
- Beiträge aktualisieren...
golotyuk at gmail dot com
24.12.2009 22:40
Manuall php installation on Debian/Ubuntu will require you to:

apt-get install libpspell-dev
8.09.2008 4:06
Debian to install the package (English dictionary):

> apt-get install php5-pspell
> apt-get install aspell-en

restart apache
dylan at wedefy dot com
19.10.2007 2:36
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...
= pspell_new('en','canadian','','utf-8',PSPELL_FAST);

spellCheckWord($word) {
$autocorrect = TRUE;

// Take the string match from preg_replace_callback's array
$word = $word[0];
// Ignore ALL CAPS
if (preg_match('/^[A-Z]*$/',$word)) return $word;

// Return dictionary words
if (pspell_check($pspell,$word))

// Auto-correct with the first suggestion, color green
if ($autocorrect && $suggestions = pspell_suggest($pspell,$word))
'<span style="color:#00FF00;">'.current($suggestions).'</span>';
// No suggestions, color red
return '<span style="color:#FF0000;">'.$word.'</span>';

spellCheck($string) {

spellCheck('PHP is a reflecktive proegramming langwage origenaly dezigned for prodewcing dinamic waieb pagges.');
OUTPUT: PHP is a reflective programming language originally designed for producing dynamic Web pages.
12.04.2007 7:12
To get round the error on Windows where the word list can't be found inside functions, take the call to pspell_new() out of the function and pass the return value to your function, i.e.

function testSpell($pspell_link) {
pspell_check($pspell_link, "test") ? 'OK' : 'Not OK';
$pspell_link = pspell_new("en");
Alan Dunsmuir
14.03.2007 10:28
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.
sholland at napervillegi dot com
28.10.2006 20:48
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.
    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<>"") {
$string=substr_replace($string, $post, $offset+strlen($word), 0);
$string=substr_replace($string, $pre, $offset, 0);
$offset=$offset+strlen($word)+strlen("$pre $post");   

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.
andrew at bluerhinos dot co dot uk
3.05.2006 12:28
If you use red hat or fedora, you may need to is use

  yum install pspell-devel 

(fixes: configure: error: Cannot find pspell)
jeremy hepler
29.03.2006 22:01
compiling php with pspell on debian sarge:

apt-get install aspell libaspell15 libaspell-dev libpspell-dev

configure php:

./configure <your other options> --with-pspell=/usr

make & install

if previous tries have failed or it is not functioning properly, unpack the php source again and configure / compile in a fresh tree
beau_scott at hotmail dot com
30.12.2005 22:13
hehe, forgot to remove the echo $string line... whoops ;)

Should be:

     * Checks spelling of $string. Whole phrases can be sent in, too, and each word will be checked.
     * Returns an associative array of mispellings and their suggested spellings
     * @param string $string Phrase to be checked
     * @return array
function checkSpelling ( $string )
// Make word list based word boundries
$wordlist = preg_split('/\s/',$string);

// Filter words
$words = array();
$i = 0; $i < count($wordlist); $i++)
$word = trim($wordlist[$i]);
preg_match('/[A-Za-z]/', $word))
$word = preg_replace('/[^\w\']*(.+)/', '\1', $word);
$word = preg_replace('/([^\W]*)[^\w\']*$/', '\1', $word);
$word = trim($word);
in_array($word, $words, true))
$words[] = $word;

$misspelled = $return = array();
$int = pspell_new('en');

        foreach (
$words as $value)
            if (!
pspell_check($int, $value))
$misspelled[] = $value;

        foreach (
$misspelled as $value)
$return[$value] = pspell_suggest($int, $value);

Gregory Boshoff
14.05.2005 1:23
If you are receiving the error message:
PSPELL couldn't open the dictionary. reason:
No word lists can be found for the language "en".

Add following lines prior to calling pspell_check:
$pspell_config = pspell_config_create("en");
$pspell_link = pspell_new_config($pspell_config);
flint at modulusgroup dot com
29.12.2004 23:19
For those will problems installing on Win32 with IIS:

Symptom: browser just hangs when calling pspell function. Running PHP.exe from command line causes pspell to claim that there is a corrupt file. Running Aspell works just fine on the command line.

You need new data files:

You can download and get more information about it here:

Just replace the data directory in C:\Programs Files\Aspell\ with the new data directory you downloaded.

No more problems.
28.09.2004 19:31
In response to csnyder's comment about calling aspell instead of using the aspell or pspell libs:

If you are running a low traffic site this will work fine and it's a good idea.

If, however, you have many customers using your spell checking script, this method does not scale well and you should consider spending the time to make the libs work properly.
webmaster at script-tease dot net
12.04.2004 4:26
Easy way to make a spellchecker:


function spellcheck ( $string ) {
$words = preg_split('/[\W]+?/',$string);
$misspelled = $return = array();
$int = pspell_new('en');
    foreach (
$words as $value) {
        if (!
pspell_check($int, $value)) {
$misspelled[] = $value;
    foreach (
$misspelled as $value) {
$return[$value] = pspell_suggest($int, $value);

jrweir _a_t_ yahoo
30.03.2004 9:15
I had the same problems after following the detailed instructions for installing on windows but was able to solve them by doing the following:

first, I copied all the files in the data directory of the aspell install (ie. C:\Program Files\Aspell\data) to a unix machine and ran dos2unix on all of the files and then copied them back.

second, added this line before the pspell_new("en") call

hope this works for you too.
kevina at gnu dot org
12.01.2003 11:19
Aspell Author Here.

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.
csnyder at chxo dot com
11.10.2002 9:37
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:
(there is a view-source option)

Not sure about Linux or Windows, but installing aspell on FreeBSD was trivially easy from the ports collection. No doubt such a script could be easily modified to use ispell instead if you'd rather.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)