我正在尝试使PHP拼写检查应用程序正常运行,但是当我尝试使用Enchant扩展程序时,却无法通过它检查单词是否存在拼写错误。
Web服务器配置
在php.ini文件中,我启用了Enchant扩展名。例如:
extension=php_enchant.dll
Run Code Online (Sandbox Code Playgroud)
样例代码:
$broker = enchant_broker_init();
$tag = 'en_US';
$bprovides = enchant_broker_describe($broker);
echo "Current broker provides the following backend(s):\n";
print_r($bprovides);
$dicts = enchant_broker_list_dicts($broker);
echo "Current broker provides the following dictionaries:\n";
print_r($dicts);
enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\php5.4.7\lib\enchant\MySpell');
if (enchant_broker_dict_exists($broker, $tag)) {
$dict = enchant_broker_request_dict($broker, $tag);
$word = 'soong';
$isCorrectlySpelled = enchant_dict_check($dict, $word);
if ($isCorrectlySpelled !== true) {
$suggestions = enchant_dict_suggest($dict, $word);
echo nl2br(print_r($suggestions, true));
} else {
echo 'The word is correctly spelt!';
}
}
enchant_broker_free($broker);
Run Code Online (Sandbox Code Playgroud)
返回值:
Current broker provides the following backend(s):
Array
(
[0] => Array
(
[name] => ispell
[desc] => Ispell Provider
[file] => C:\php5.4.7\libenchant_ispell.dll
)
[1] => Array
(
[name] => myspell
[desc] => Myspell Provider
[file] => C:\php5.4.7\libenchant_myspell.dll
)
)
Current broker provides the following dictionaries:
Run Code Online (Sandbox Code Playgroud)
但是,这不能告诉我“ soong”一词的拼写是否正确!
事实证明,让 Enchant 扩展在 Windows、IIS 和 PHP 5.4.7 中运行非常容易!
您所需要做的就是创建一些文件夹,下载一些字典文件,它的效果非常好!
转至https://wiki.mozilla.org/L10n:Dictionaries并下载您要进行拼写检查的词典。
然后在 PHP 文件夹中创建此目录结构: [PHP]\share\myspell\dicts
最后,将 *.aff 和 *.dic 文件(例如 en_US.aff 和 en_US.dic)放入 dicts 文件夹中,然后就可以了!
现在上面的代码返回字典信息,加上拼写建议!
Current broker provides the following backend(s):
Array
(
[0] => Array
(
[name] => ispell
[desc] => Ispell Provider
[file] => C:\php5.4.7\libenchant_ispell.dll
)
[1] => Array
(
[name] => myspell
[desc] => Myspell Provider
[file] => C:\php5.4.7\libenchant_myspell.dll
)
)
Current broker provides the following dictionaries:
Array
(
[0] => Array
(
[lang_tag] => en_GB
[provider_name] => myspell
[provider_desc] => Myspell Provider
[provider_file] => C:\php5.4.7\libenchant_myspell.dll
)
[1] => Array
(
[lang_tag] => en_US
[provider_name] => myspell
[provider_desc] => Myspell Provider
[provider_file] => C:\php5.4.7\libenchant_myspell.dll
)
)
Array
(
[0] => suing
[1] => sung
[2] => goons
[3] => song
[4] => soon
[5] => soon g
)
Run Code Online (Sandbox Code Playgroud)
学分:
http://www.php.net/manual/en/enchant.examples.php#109925
http://my.opera.com/iwanluijks/blog/using-enchant-with-php-on-windows-part-1
| 归档时间: |
|
| 查看次数: |
3575 次 |
| 最近记录: |