Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MySQL在搜索中包含可选拼写_Php_Mysql - Fatal编程技术网

PHP/MySQL在搜索中包含可选拼写

PHP/MySQL在搜索中包含可选拼写,php,mysql,Php,Mysql,我的PHP搜索表单从MySQL数据库中提取数据。我希望用户有时会在搜索框中填入一个与我的数据库条目拼写稍有不同的搜索词,比如“theater”而不是“theater”。我希望这些词中只有少数非常常见,因此我在数据库表中添加了一行,其中包含这些可选拼写,我的PHP搜索表单也搜索数据库的这一行。它工作得很好,但在维护数据库时,这将导致大量额外的工作,因此我想知道在我的PHP代码中是否可以做些什么来搜索那些预定义的替代拼写(我不是要给用户建议的拼写,但我希望搜索表单返回,例如,包含“剧院”的条目)即使

我的PHP搜索表单从MySQL数据库中提取数据。我希望用户有时会在搜索框中填入一个与我的数据库条目拼写稍有不同的搜索词,比如“theater”而不是“theater”。我希望这些词中只有少数非常常见,因此我在数据库表中添加了一行,其中包含这些可选拼写,我的PHP搜索表单也搜索数据库的这一行。它工作得很好,但在维护数据库时,这将导致大量额外的工作,因此我想知道在我的PHP代码中是否可以做些什么来搜索那些预定义的替代拼写(我不是要给用户建议的拼写,但我希望搜索表单返回,例如,包含“剧院”的条目)即使用户在其中键入“theater”。是否有一种简单的方法可以做到这一点(没有搜索服务器)?

是的,您可以轻松完成这项工作,而无需数据库搜索,您需要正确的拼写,因此我建议您通过PHP编码而不是数据库搜索来完成这项工作

您可以使用PHP Pspell模块完成这项工作,PHP Pspell就像android键盘一样,每当在搜索框中输入拼写错误时,它会自动检查字典中的拼写,并使其正确,就像用户输入“theater”,然后用“theater”自动更正一样

在开始编程之前,您必须检查是否安装了Pspell模块

<?php
$config_dic= pspell_config_create ('en');


我唯一的想法就是检查搜索词的拼写并自动搜索拼写检查器的最佳猜测以及实际搜索的内容。谢谢,但我希望避免向用户建议拼写。另一个问题是,我想到的单词实际上不是拼写错误,而是其他拼写错误,我的数据库可能两者都有。例如,一个theat我的数据库中的res称自己为“XX剧院”,另一个称自己为“YY剧院”-我希望我的搜索同时返回它们,无论用户使用的是“剧院”还是“剧院”作为搜索词。亲爱的,您使用MySql查询来完成这项工作,例如
从表中选择列,其中列类似“%word”;
<?php
function orthograph($string)
{
    // Suggests possible words in case of misspelling
    $config_dic = pspell_config_create('en');

    // Ignore words under 3 characters
    pspell_config_ignore($config_dic, 3);

    // Configure the dictionary
    pspell_config_mode($config_dic, PSPELL_FAST);
    $dictionary = pspell_new_config($config_dic);

    // To find out if a replacement has been suggested
    $replacement_suggest = false;

    $string = explode('', trim(str_replace(',', ' ', $string)));
    foreach ($string as $key => $value) {
        if(!pspell_check($dictionary, $value)) {
            $suggestion = pspell_suggest($dictionary, $value);

            // Suggestions are case sensitive. Grab the first one.
            if(strtolower($suggestion [0]) != strtolower($value)) {
                $string [$key] = $suggestion [0];
                $replacement_suggest = true;
            }
        }
    }

    if ($replacement_suggest) {
        // We have a suggestion, so we return to the data.
        return implode('', $string);
    } else {
        return null;
    }
}
<?php
$search = $_POST['input'];
$suggestion_spell = orthograph($search);
if ($suggestion_spell) {
    echo "Try with this spelling : $suggestion_spell";
}

$dict = pspell_new ("en");
if (!pspell_check ($dict, "lappin")) {
    $suggestions = pspell_suggest ($dict, "lappin");
     foreach ($suggestions as $suggestion) {
        echo "Did you mean: $suggestion?<br />";
     }
}
// Suggests possible words in case of misspelling
    $config_dic = pspell_config_create('en');

    // Ignore words under 3 characters
    pspell_config_ignore($config_dic, 3);

    // Configure the dictionary
    pspell_config_mode($config_dic, PSPELL_FAST);
$dictionary = pspell_new_config($config_dic);
$config_dic = pspell_config_create ('en');
pspell_config_personal($config_dic, 'path / perso.pws');
pspell_config_ignore($config_dic , 2);
pspell_config_mode($config_dic, PSPELL_FAST);
$dic = pspell_new_config($config_dic);
pspell_add_to_personal($dic, "word");
pspell_save_wordlist($dic);
?>