我试图比较数据库和输入表单中的单词。我想数相同的单词,但结果不正确。
输入:方法或算法
tb_keyword
id key
1 method
2 algorithm
结果
1
预期结果
2
代码:
include_once("kmp.php");
$kata = '';
if(isset($_GET['kata']))
$kt = $_GET['kata'];
?>
<div style="width:600px;">
<form method="get" action="">
Cari Kata : <input type="text" name="kata" value="<?php echo $kata; ?>" /
<input type="submit" value="Cari">
</form>
</div>
<?php
$kata = explode(' ',$kt);
foreach($kata as $katas){
$total = 0;
$KMP = new KMP();
$art = $db_con->prepare("select * from tb_positive_keywords");
$art->execute();
while($teks = $art->fetch(PDO::FETCH_ASSOC)){
if($kata!=''){
$hasil = $KMP->KMPSearch($katas,$teks['positive_keyword']);
$total += count($hasil);
echo "Kata yang dicari adalah : ".$katas."<br/>";
echo "Jumlah kata yang ditemukan : ".count($hasil)."<br/>";
echo "Yaitu pada posisi string ke : ";
foreach($hasil as $h) echo $h." ";
echo "<br/>";
}
echo "<div style='width:600px;'>";
echo nl2br(str_replace($katas,"<font color='red'>".$katas."</font>",$teks['positive_keyword']));
echo "</div>";
echo $total;
}