我正在尝试在我的网络应用程序中实现搜索功能,我想要做的是在数据库中搜索一个单词,如果它不存在,系统应该告诉我或找到我最接近的单词。我想要在数据库中搜索名称而不是在数组中搜索以下是我的index.php
<html>
<body>
<form method="post" action="two.php">
Search : =<input type="text" name="name" id="x" autocomplete="off">
<input type="submit" name="submit" id="submit" value="Search">
</form>
</html>
这是我的two.php
代码
<?php
$input = $_POST[name];
$words = array('apple','pineapple','banana','orange',
'radish','anything','carrot','pea','bean','potato');
$shortest = -1;
foreach ($words as $word) {
if ($input == $word) {
$closest = $word;
$shortest = 0;
break;
}
$lev = levenshtein($input, $word);
if ($lev <= $shortest || $shortest < 0) {
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
?>
这是我搜索名称
的数据库的结构create database search
create table emp ( `id` int(11) NOT NULL auto_increment,
`name` varchar(30) default NULL,
PRIMARY KEY (`id`));