I have millions of string data in mysql table and have to cross check with the string one by one with the table, if matches return true or else false. I tried with simple preg_match as below, which is consuming more memory and time.
<?php
$message = 'Hi xyz your account 123 credited Rs. 456 available balance is 789';
$template = "/Hi .+? your account .+? credited Rs. .+? available balance is .+?/";
$sql = "SELECT * FROM templates";
$result = $conn->query($sql);
$flag = false;
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$template = $row["template"];
if (preg_match($template, $message)) {
$flag = true;
break;
}
$flag = false;
}
$flag = false;
}
return $flag;
And also I tried with php_aho_corasick But no use. Please suggest some good way to deal with the problem. Thanks in advance.
Edit : We have millions of templates and here are the sample templates
$template1 = "/Hi .+? your account .+? credited Rs. .+? available balance is .+?/";
$template2 = "/Hi .+? your account .+? credited Rs. .+? available balance is .+? get more upate on/";
$templateN = "/Hello .+? click the link .+? to get your available balance./";
$message = "Hi xyz your account 123 credited Rs. 456 available balance is 789";
In the $message xyz, 123, 456, and 789
are the dynamic values which will change, now have to cross check with N number of templates which will matches with the message. If we replace .+?
in template1 with message then will get exact match, while template2 has additional words and templateN differs completely. So which will be the better way to handle such scenarios.