如何检查字符串数组以匹配PHP中的参考字符串?

问题描述

我在MysqL表中有数百万个字符串数据,如果匹配项返回true或false,则必须与该表一一对应地进行交叉检查。 我尝试使用以下简单的preg_match,这会消耗更多的内存和时间。

<?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;

我也尝试使用php_aho_corasick,但没有用。请提出一些解决问题的好方法。 预先感谢。

编辑::我们有数百万个模板,这是示例模板

$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";

$message xyz,123,456,and 789中的动态值将更改,现在必须交叉检查N个与消息匹配的模板。如果我们将模板1中的.+?替换为message,则将获得完全匹配,而模板2具有其他单词,而模板N则完全不同。因此,这将是处理此类情况的更好方法

解决方法

我认为您需要运行此查询;

SELECT * FROM `templates` WHERE message LIKE 'Hi % your account % credited Rs. % available balance is %'

所有结果都将与您的搜索匹配,而无需使用“ preg_match”命令

,

尝试一下,让我们知道结果;它似乎可以在我的服务器上正常工作,而无需增加资源。在您的大桌子上尝试

$message = 'Hi xyz your account 123 credited Rs. 456 available balance is 789';

$sql5  = "SELECT template FROM templates ";
$sql5 .= "WHERE '$message' LIKE REPLACE( REPLACE(template,'.+?','%'),'/','') ";
$qry5 = $connect->query($sql5) or trigger_error($connect->error,E_USER_ERROR);
$cnt5 = $qry5->num_rows;
    /* 
    echo $cnt5."<br>";
    //SHOWING RESULTS OF TEMPLATES THAT MATCH the message
    while($inf5 = $qry5->fetch_assoc()){
        $template = $inf5['template'];
        
        echo $template."<br>";
    }
    */

$flag = false;
if($cnt5>0){
    $flag = true;
}