我有两个表,方案如下:
tb_keywords
id_keyword
keyword
tb_post
id_post
description
我想在php中实现sting匹配的暴力算法。如果描述与关键字匹配/相似,则此描述将插入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";
}
}
要查看文本中是否有特定单词,可以使用带有单词边界的正则表达式。
即
preg_match("/''bPHP''b/","PHP中的regex")#匹配中的单词"PHP"字符串
$description = trim($_POST['description']);
$check = $db_con->prepare("SELECT * FROM tb_keywords");
$check->execute();
while($row = $check->fetch(PDO::FETCH_ASSOC)) {
$search = $row['keyword'];
if (preg_match("/'b$search'b/", $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";
}
}
}