Example #1 Enchant Usage Example
<?php
$tag = 'en_US';
$r = enchant_broker_init();
$bprovides = enchant_broker_describe($r);
echo "Current broker provides the following backend(s):\n";
print_r($bprovides);
$dicts = enchant_broker_list_dicts($r);
print_r($dicts);
if (enchant_broker_dict_exists($r,$tag)) {
$d = enchant_broker_request_dict($r, $tag);
$dprovides = enchant_dict_describe($d);
echo "dictionary $tag provides:\n";
$wordcorrect = enchant_dict_check($d, "soong");
print_r($dprovides);
if (!$wordcorrect) {
$suggs = enchant_dict_suggest($d, "soong");
echo "Suggestions for 'soong':";
print_r($suggs);
}
enchant_broker_free_dict($d);
} else {
}
enchant_broker_free($r);
?>
edward dot 81 at gmail dot com (2012-12-07 18:25:35)
If you pass a word with special chars on it (es. òàùèì) you have to convert the string to utf8 or enchant don't recognize correctly the word.
Es.
$word = "perché";
enchant_dict_check($dict, utf8_encode($key));
robert dot johnson at icap dot com (2012-09-03 12:49:24)
Here is help for Windows users:
You need to add dictionaries to your computer for Enchant.
1. Enchant looks in your registry keys, I don't know what keys it wants, but it looks here - I ignored all of these:
* Default User\Software\Enchant\Config
* Default User\Software\Enchant\Ispell
* Default User\Software\Enchant\Myspell
2. It looks for OpenOffice dictionaries (from the registry settings for OpenOffice)
3. It looks in folder [PHP]\share\myspell\dicts
I got it working by copying the en-us dictionary files from Firefox into \share\myspell\dicts, and renaming them en_US.*. You can download and install dictionaries from OpenOffice from here I think: http://extensions.services.openoffice.org/dictionary
Enchant creates and writes to the following folder, so you must allow PHP read and write permissions to: [SYSTEM32]\config\systemprofile\Application Data\enchant
It would be convenient if Enchant could accept parameters to specify the location of the main dictionaries and user-dictionaries, I suppose the registry keys are the only way to do it.
Anonymous (2012-07-19 16:57:43)
So that you don't need to track down dictionaries to use with Enchant, here are some notes for installing aspell dictionaries on your Mac using Macports.
Install Enchant (this should also install aspell as a dependency):
sudo port install php5-enchant
Find available dictionaries:
port search aspell-dict
Install the English dictionary:
sudo port install aspell-dict-en
nils dot rocine at gmail dot com (2012-06-29 07:03:43)
I installed Enchant through MacPorts (port php5-enchant.)
Testing the example: Enchant found 3 brokers: aspell, ispell, and myspell. But found no dictionaries.
I'm not sure if the providers were installed by the php5-enchant port, or from some other port I previously installed. The bins for these apps live in /opt/local--the default MacPorts install directory--so I don't think they came with my distribution (OS X.)
To run the sample code to the results described, I installed the aspell English dictionary from ftp://ftp.gnu.org/gnu/aspell/dict/0index.html and installed with the standard pattern: tar -xzvf [download] then cd [dictionary dir] and ./configure ; make ; sudo make install
iwanluijks at gmail dot com (2010-10-13 04:29:47)
For more information on Enchant, check my latest blog-post at http://bit.ly/cGwmb7 . It describes setting up Enchant with PHP on Windows.