PHP 8 - Enchant not working - Broker returns an empty array

498 views Asked by At

I am using PHP 8 on Xampp (Windows)

Have added extension=php_enchant.dll to php.ini

php_info

  • php directory added to windows PATH
  • copied libenchant_hunspell.dll / libenchant_ispell.dll / libenchant_myspell.dll to [php]/lib/enchant
  • moved *.dic & *.aff to [php]/share/myspell/dicts

But still cannot get Enchant to detect Brokers

sample code

<?php
$broker = enchant_broker_init();
$tag = 'en_US';

$bprovides = enchant_broker_describe($broker);
echo "Current broker provides the following backend(s):\n";
print_r($bprovides);

Output

Current broker provides the following backend(s): Array ( ) Current broker provides the following dictionaries: Array ( )
2

There are 2 answers

3
jriffel73 On

I ran into the same problem. I am moderately skilled in the PHP core and after a few days of debugging this I finally got it to work. Unfortunately, there seem to be quite a few changes in both lib_enchant and in PHP 8.

lib_enchant 2.x and PHP 8 (on Windows) seem to only support the hunspell provider (PHP 7.x and previous was using both ispell and myspell providers). Further, it seems that enchant now hard codes the path to the provider DLLs to:

C:\usr\local\lib\enchant-2

So, you must locate libenchant2_hunspell.dll which is in the PHP distro in the lib/enchant folder to:

C:\usr\local\lib\enchant-2\libenchant2_hunspell.dll

Similarly the location of the dictionary files defaults to:

C:\usr\local\share\enchant

So in my case I ended up with the following files in that folder:

C:\usr\local\share\enchant\en_US.aff
C:\usr\local\share\enchant\en_US.dic
C:\usr\local\share\enchant\es_ANY.aff
C:\usr\local\share\enchant\es_ANY.dic

In my review of the sources for enchant it seems you might be able to control the location of the spell check dictionaries via the environment variable ENCHANT_CONFIG_DIR but I believe the provider location is now hard coded. This is new with lib_enchant 2.x and PHP 8 ... and a huge hassle for us because we do not want to install software in c:\usr\local on a Windows machine.

I will probably work up a patch and submit it to PHP to fix this but I am concerned they will not accept it because the patch will really be in lib_enchant. PHP does host their own copy of lib_enchant on github for building dependencies like this so perhaps they will integrate the changes.

I hope this helps others.

0
Oleshek On

I have no enauf reputation to leave comment for jriffel73 answer so I create a new answer.

I too faced with the problem that dictionaries array is empty after all manipulations in jriffel73 answer. So I just changed path for dictionaries a little bit and it worked.
From:

C:\usr\local\share\enchant\en_US.aff

To:

C:\usr\local\share\enchant\hunspell\en_US.aff

I also granted all rights to these files