我有2個表和方案是這樣的:如何在php中實現字符串匹配的蠻力算法?
tb_keywords
id_keyword
keyword
tb_post
id_post
description
我想要實現在PHP蜇匹配蠻力算法。如果描述符合/類似於關鍵字,則此描述將插入到tb_post中。我試圖做這個工作,但它不工作。 沒有錯誤信息,只是給出了空白的結果。
HTML
<textarea type="text" class="form-control" name="description" id="description" placeholder="Description" required /></textarea>
PHP
$description = trim($_POST['description']);
$check = $db_con->prepare("SELECT * FROM tb_keywords");
$check->execute();
$row=$check->fetch(PDO::FETCH_OBJ);
$positive = $row->keyword;
function brute_force($positive, $description)
{
$n = strlen($description);
$m = strlen($positive);
for ($i = 0; i < $n-$m; $i++) {
$j = 0;
while ($j < $m && $description[$i+$j] == $positive[$j]) {
$j++;
}
if ($j == $m) {
return $i;
}
return -1;
}
$find[$i]=brute_force($positive, $description);
$create=$db_con->prepare("INSERT INTO tb_post(description) VALUES(:description)");
$create->bindParam(":description", $description);
$create->execute();
$row=$create->rowCount();
if($row>0) {
echo "success";
} else {
echo "fail";
}
}
你的代碼有一些問題。它永遠不會繼續'for'循環,並且永遠不會到達'for'循環之後的部分代碼。據我瞭解,您必須嘗試在說明中找到關鍵字,但對於不區分大小寫的情況,使用[stripos()](http://php.net/manual/ru/function.stripos.php)會更容易搜索。 – Wizard