以下是如何在您不想或無法使用系統上安裝的字典時使用 pspell 的範例。
<?
$text_to_check = 'I can sspeak English';
// 選擇性。稍微清理一下文字
$clean_text_to_check = preg_replace('/[^a-z0-9\-\.!;]+/i', ' ', $text_to_check);
// 取得單字列表
$word_list = preg_split('/\s+/', $clean_text_to_check);
$pspell_config = pspell_config_create("en", null, null, 'utf-8');
// 如果您想要的 aspell 字典尚未安裝,
// 請複製 aspell 字典並在此設定字典的路徑
pspell_config_data_dir($pspell_config, "/home/alex/dictionaries/");
pspell_config_dict_dir($pspell_config, "/home/alex/dictionaries/");
$pspell_link = pspell_new_config($pspell_config);
foreach($word_list as $word) {
if (!pspell_check($pspell_link, trim($word))) {
$suggestions = pspell_suggest($pspell_link, trim($word));
echo $word . ' 拼字錯誤 <br />';
foreach ($suggestions as $suggestion) {
echo "\t 可能的拼法:$suggestion <br />";
}
}
} else {
// 拼寫正確
}
}
?>